How to redirect www to naked domain and vice versa with NGINX?
3rd February 2014 | by Adam Beres-Deak | nginx, redirection
One thing almost every website needs is redirection. Many websites decide to serve their visitors both over www and non-www site, just in case the user types it into the browser. But for SEO it's bad, when you have the same site over two different domains. Here is, how to solve this issue with NGINX.
Redirecting www to non-www with if
statement
server {
listen 80;
server_name www.example.com example.com;
if ($host = 'www.example.com' ) {
# redirecting www.example.com to example.com
# path, query string are retained
rewrite ^/(.*)$ http://example.com/$1 permanent;
}
}
Please note that if
is considered evil inside NGINX configuration, but it is perfectly OK in this case. The official docs say, that there are two cases when if
is "100% safe":
- redirect (our case)
- return
if ($request_method = POST ) {
return 405;
}
if ($args ~ post=140){
rewrite ^ http://example.com/ permanent;
}
Redirecting www to non-www without if
statement
The trick in this case is that we have to define two server blocks.
server {
listen 80;
server_name www.example.com;
# redirecting www.example.com to example.com
# path, query string are retained
return 301 http://example.com$request_uri;
}
server {
listen 80;
server_name example.com;
...
}
Redirecting non-www to www
server {
listen 80;
server_name example.com;
# redirecting example.com to www.example.com
# path, query string are retained
return 301 http://www.example.com$request_uri;
}
server {
listen 80;
server_name www.example.com;
...
}
Latest blog posts
Displaying icons with custom elements 14th October 2015
Plain JavaScript event delegation 26th January 2015
After the first year of blogging - what happened on my blog in 2014? 1st January 2015
Better webfont loading with using localStorage and providing WOFF2 support 18th December 2014