Nginx, 301 redirect for all occasions

May 31, 2016 14 Yehor Rykhnov

This article describes several examples of 301-redirect for Nginx

Nginx, 301 redirect from the http protocol to the https protocol

If you has an SSL certificate for a domain, you can configure the https protocol. After that, for the 301 th redirect, you need to add the following code in the nginx configuration file for a domain:

server {
    #...
    if ($scheme = http) {
        return 301 https://$server_name$request_uri;
    }
}

or

server {
    #...
    listen  server_ip:80;
    server_name  www.devreadwrite.com;
    rewrite ^ https://www.devreadwrite.com$request_uri? permanent; 
}

Nginx, 301 redirect with protocol https to http

Reverse configuration example for a redirect from http to https protocol:

server {
   listen  443;
   server_name  www.devreadwrite.com;
   rewrite ^ http://www.devreadwrite.com$request_uri? permanent; 
}

server {
    listen  80;
    server_name www.devreadwrite.com;
    #...
}

Nginx, redirect from the www domain on without the www

301 redirect with www domain to the non-www domain this called canonization or gluing:

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

or

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

Nginx, 301 redirect to the domain without the www to the www with domain

Redirect from Non-www to www:

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

server {
    listen  80;
    server_name www.devreadvrite.com;
    #...
}

Nginx, 301 redirect from single page

If URL of page has changed, then better to make a 301 redirect to the new URL of page:

server {
    #...
    if ( $request_filename ~ oldpage/ ) {
        rewrite ^ http://www.devreadvrite.com/newpage/? permanent;
    }
    #...
}

Nginx, 301 redirects for folder

A similar example of 301-redirect for folder:

server {
    #...
    if ( $request_filename ~ oldfolder/.+ ) {
        rewrite ^(.*) http://www.devreadvrite.com/newfolder/$1 permanent;
    }
    #...
}

Nginx, 301 redirects from all pages of one domain to the main page of another domain

If you have changed your domain name, then you can make a 301 redirect from the old domain to the new:

server {
    server_name domain.com www.devreadvrite.com; 
    rewrite ^ $scheme://www.new-devreadvrite.com;
}

Nginx, 301 redirects from each page of one domain to the same URL address of another domain

If you plan to change your domain name, the domain redirection is the single best solution for save users and their transfer requests for the new domain.

server {
    server_name devreadvrite.com www.devreadvrite.com; 
    rewrite ^ $scheme://www.new-devreadvrite.com$request_uri permanent;
}

Nginx, 301 redirect from the pages with a slash to the page without a slash at the end of the URL

It often happens that the same page is available in two URL, for example /may-best-page and /my-best-page/, if the humans understands that this is one and the same page, the search engines are aware of this as two different pages. To avoid this, you can make a 301 redirect from the pages with a slash at the end of the URL on without him:

server {
    #...
    rewrite ^/(.*)/$ /$1 permanent;
    #...
}

Nginx, 301 redirect pages with no slash on the page with a slash in the end of the URL

The reason to do this redirection is the same as that in the situation described above, except that you need to do a redirect from the page without a slash at the end of a URL to a page with a slash at the end of the URL:

server {
    #...
    rewrite ^(.*[^/])$ $1/ permanent;
    #...
}

Additionally

Before use of examples change domain devreadwrite.com to your domain name. After making changes to the nginx configuration file for a domain, you must restart nginx:

service nginx reload

or

service nginx restart

If you have Apache installed on the server, then your can read examples of 301-redirect with help .htaccess here: 301 redirect for all occasions using .htaccess.