Nginx

Point Your Www To Non Www With Efficient 301 Redirection For Nginx

Posted by Weston Ganger on July 08, 2014

This is extremely important for SEO that you do not have your site working for both www and non-www because it will split your ranking between the two as if they were seperate websites

In order to fix this on Nginx the old and outdated way is as follows:


server{
  listen 80;
  server_name www.westonganger.com;
  rewrite ^/(.*)$ http://westonganger.com/$1 permanent;
}
server{
  listen 80;
  server_name westonganger.com;
  ...the rest of your server config...
}

The new efficient and proper way is as follows:


server{
  listen 80;
  server_name www.westonganger.com;
  return 301 $scheme://westonganger.com$request_uri;
}
server{
  listen 80; 
  server_name westonganger.com
  ...the rest of your server config...
}

Take note that these instructions are to point www to non-www, if you would like to point non-www to www then just change the server_names and return statements accordingly.


Posted in Linux and Tagged with nginx redirection 


Need help on your next project or application?

I specialize in Ruby-on-Rails, AngularJS, Javascript, Bootstrap, and Hybrid Mobile Apps with Cordova & Ionic.

Contact Me

Recommended Posts