301 Permanent Redirect NON WWW to WWW and viceversa on Nginx

Valic —  December 5, 2011 — 2 Comments

This is my favorite and I think the more simple  if you want permanent redirect  from NON WWW to WWW insert the following code in the nginx configuration file nginx.conf :

server {
  listen 80;
  server_name www.debian-tutorials.com;
  rewrite ^/(.*)$ http://debian-tutorials.com/$1 permanent;
}
Permanent redirect from WWW to NON WWW in nginx, use this code in your nginx.conf :
server {
  listen 80;
  server_name debian-tutorials.com;
  rewrite ^/(.*)$ http://www.debian-tutorials.com/$1 permanent;
}

Enjoy!

Valic

Posts Twitter Facebook

Editor in Chief at Debian-Tutorials, Linux enthusiast.

2 responses to 301 Permanent Redirect NON WWW to WWW and viceversa on Nginx

  1. Anytime I had that 1 line in either /etc/nginx/sites-available/default or /etc/nginx/nginx.conf …. It tells me my config is invalid. Oh well.

  2. You can use this:

    if ($host = ‘debian-tutorials.com’ ) {
    rewrite ^/(.*)$ http://www.debian-tutorials.com/$1 permanent;
    }

Leave a Reply