Nginx redirect www.example.com requests to example.com or vice versa

Add following configuration directive if you want to redirect www.example.com to example.com

server {
    server_name  www.example.com;
    rewrite ^(.*) http://example.com$1 permanent;
}

server {
    server_name  example.com;
    Put here your domain hosting configuration.
}

Add following configuration directive if you want to redirect example.com to www.example.com

server {
    server_name  example.com;
    rewrite ^(.*) http://www.example.com$1 permanent;
}

server {
    server_name  www.example.com;
    Put here your domain hosting configuration.
}

Comments 23

  1. Justin wrote:

    This is was I was looking for, thanks for posting. I wrote about it and included this url in my post as a reference.

    Posted 01 Mar 2008 at 5:22 pm
  2. vanderkerkoff wrote:

    How do you achieve what I believe is called ‘masking’ using this technique?

    When the user enters example.com into the address bar they go to http://www.example.com but example.com stays in the address bar?

    Anyone got any ideas?

    Posted 25 Jun 2008 at 2:39 pm
  3. Colin wrote:

    Yer the bomb! this totally fixed my current conundrum… thanks again!

    Posted 27 Jul 2008 at 9:17 am
  4. Yann wrote:

    I am grateful. This was a no pain solution to my problem.

    Posted 13 Aug 2008 at 2:16 pm
  5. Travis Reeder wrote:

    Exactly what I needed. Thanks!

    Posted 02 Oct 2008 at 2:03 am
  6. Christopher Fritz wrote:

    Nice. This is exactly what I needed. Thanks for providing this information for others! Perhaps because of my set up, I had to include the “listen” line, so my redirect looks like (using a fake IP address in this example):

    server {
    listen 120.120.120.120:80;
    server_name http://www.example.com;
    rewrite ^(.*) http://www.example.com$1 permanent;
    }

    server {
    listen 120.120.120.120:80;
    server_name example.com;
    …etc…
    }

    In other words, if the main server {} area has the “listen”, be sure to add it to the server {} area with the redirect. (It really does make sense, but it can be easy to miss!)

    Posted 03 Oct 2008 at 4:49 am
  7. indiehead wrote:

    you are a genius!

    thanks for this, really needed it,

    John.

    Posted 15 Oct 2008 at 12:40 pm
  8. Dave wrote:

    Thank you, it really helped me with my site setup!

    Posted 30 Jul 2009 at 8:51 pm
  9. Rodion wrote:

    Thanks for these hints about nginx. That saved me lots of time! :)

    Posted 16 Oct 2009 at 10:51 pm
  10. Jonathan wrote:

    Worked great, many thanks! Like Christopher I had to include the listen directive.

    Posted 19 Oct 2009 at 2:28 am
  11. Computer File Recovery wrote:

    I to did exactly what this page says but it didn’t work until I added the listen line as well. Thanks Chris for the extra input, and thanks Aleks for making this page!

    Posted 23 Oct 2009 at 7:53 am
  12. James wrote:

    So simple, yet couldn’t figure it out. Thanks for the time savings. It was driving me nuts!

    Posted 09 Nov 2009 at 3:16 am
  13. Adrien Lamothe wrote:

    Neither of those work for my server. I’ve been trying different variants of the redirect regex, including the one you suggest here, and none of them work. Other than that nginx is working fine. Very bizarre.

    If my server didn’t have the redirect library, wouldn’t Nginx complain about it at startup and die?

    Thanks,

    Adrien

    Posted 04 Jan 2010 at 1:31 am
  14. Adrien Lamothe wrote:

    Also, if I include the www rewrite rule in my module definition for the SSL module (i.e. “listen 443″), then SSL doesn’t work, but if I remove the www rewrite rule from the SSL module def then SSL does work.

    Posted 04 Jan 2010 at 1:38 am
  15. Tom Werth wrote:

    Yeah, interestingly, we ran into the same SSL issue as well. We can’t get the rewrite to work in the SSL module. We’re looking for solutions. Any help is appreciated.

    Posted 05 Jan 2010 at 10:11 pm
  16. Adrien Lamothe wrote:

    I guess we’ll just hope the newer versions fix the problem.

    Posted 28 Jan 2010 at 6:56 am
  17. Fitim Blaku wrote:

    Worked perfectly. Thanks.

    Posted 14 Apr 2010 at 1:30 am
  18. Laurent Raufaste wrote:

    Nice & clear example

    Posted 17 May 2010 at 3:54 pm
  19. voku wrote:

    if ($host ~* ^www\.(.*)) {
    set $host_without_www $1;
    rewrite ^(.*)$ http://$host_without_www$1 permanent;
    }

    Posted 04 Aug 2010 at 1:56 pm
  20. John wrote:

    You’re a star!

    just what i needed

    Posted 28 Nov 2010 at 9:20 pm
  21. Joe wrote:

    Worked great. Thanks.

    Posted 14 Feb 2011 at 11:12 am
  22. Greg wrote:

    Should be a slight alteration – see the Nginx Pitfalls page in the docs, “Taxing Rewrites” bit, for details. Basically, the rewrite could be more efficient:

    rewrite ^ http://www.example.com$request_uri? permanent;

    http://wiki.nginx.org/Pitfalls#Taxing_Rewrites

    Posted 09 Mar 2011 at 12:08 pm
  23. Vinicius wrote:

    Thanks man very helpfull

    Posted 29 May 2011 at 7:08 pm

Trackbacks & Pingbacks 3

  1. From compbrain.net » nginx redirector on 29 Jul 2009 at 3:18 am

    [...] of the server in question is to handle e-mail, no webserver was installed. After stumbling upon http://aleksandarsavic.com/nginx-redirect-wwwexamplecom-requests-to-examplecom-or-vice-versa/ I whipped up a 6 line config for nginx, and deployed it on the machine. nginx is lightweight enough [...]

  2. From platform agnostic » Deploying a Perl Dancer application on Amazon EC2 on 08 Jan 2011 at 6:55 pm

    [...] 2. to add the following server block to my nginx configuration so that http://www.stupidtwitterstats.com redirects to stupidtwtterstats.com (via) [...]

  3. From Nginx Redirect WWW to NON-WWW HOWTO | Nginx Lighttpd Tutorial on 03 Jun 2011 at 2:39 pm

    [...] Nginx redirect http://www.example.com requests to example.com or vice versa [...]

Post a Comment

Your email is never published nor shared. Required fields are marked *