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
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 ¶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 ¶Yer the bomb! this totally fixed my current conundrum… thanks again!
Posted 27 Jul 2008 at 9:17 am ¶I am grateful. This was a no pain solution to my problem.
Posted 13 Aug 2008 at 2:16 pm ¶Exactly what I needed. Thanks!
Posted 02 Oct 2008 at 2:03 am ¶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 ¶you are a genius!
thanks for this, really needed it,
John.
Posted 15 Oct 2008 at 12:40 pm ¶Thank you, it really helped me with my site setup!
Posted 30 Jul 2009 at 8:51 pm ¶Thanks for these hints about nginx. That saved me lots of time!
Posted 16 Oct 2009 at 10:51 pm ¶Worked great, many thanks! Like Christopher I had to include the listen directive.
Posted 19 Oct 2009 at 2:28 am ¶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 ¶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 ¶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 ¶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 ¶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 ¶I guess we’ll just hope the newer versions fix the problem.
Posted 28 Jan 2010 at 6:56 am ¶Worked perfectly. Thanks.
Posted 14 Apr 2010 at 1:30 am ¶Nice & clear example
Posted 17 May 2010 at 3:54 pm ¶if ($host ~* ^www\.(.*)) {
Posted 04 Aug 2010 at 1:56 pm ¶set $host_without_www $1;
rewrite ^(.*)$ http://$host_without_www$1 permanent;
}
You’re a star!
just what i needed
Posted 28 Nov 2010 at 9:20 pm ¶Worked great. Thanks.
Posted 14 Feb 2011 at 11:12 am ¶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 ¶Thanks man very helpfull
Posted 29 May 2011 at 7:08 pm ¶Trackbacks & Pingbacks 3
[...] 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. to add the following server block to my nginx configuration so that http://www.stupidtwitterstats.com redirects to stupidtwtterstats.com (via) [...]
[...] Nginx redirect http://www.example.com requests to example.com or vice versa [...]
Post a Comment