HTTP redirect (or URL redirect) - This redirects one domain or address to another. There are many uses for redirects.

A redirect is used whenever a site needs users making requests to be directed to a different address. There are many situations in which a redirect is essential.

Nginx uses several tools to redirect traffic

Nginx

The ngx_http_rewrite_module, necessary to set up redirects, is installed automatically with Nginx.

Redirect 301 from www.domain.ru to domain.ru

For Nginx you need to create two server sections in the configuration file, one for a domain with www and one for a domain without www:
Server section for the redirect:

server {  
     listen 80;
     server_name www.domain.ru;
     rewrite ^ http://domain.ru$request_uri? 
}

The server section where the main settings of the domain are located:

server {  
     listen 80;
     server_name domain.ru;
.....
}

After making changes to the Nginx configuration file, you need to restart the web server.

service nginx restart  

Redirect 301 from domain.ru to www.domain.ru

For Nginx you need to create two server sections in configuration file, one for domain without www and one for domain with www.

The server section for the redirect:

server {  
     listen 80;
     server_name domain.ru;
     rewrite ^ http://www.domain.ru$request_uri? 
}

The server section where the main settings of the domain are located.

server {  
     listen 80;
     server_name www.domain.ru;
.....
}

After making changes to the Nginx configuration file, you need to restart the web server.

service nginx restart  

Redirect 301 from https to http

For Nginx you need to create two server sections in the configuration file, one for https (443 port) and one for http (80 port).

The server section is for opening on https(443 port) and setting up the redirect:

server {  
   listen 443;
   server_name www.domain.ru;
   rewrite ^ http://www.domain.ru$request_uri? 
}

The server section, to open via http (80 port), where the main domain settings are located.

server {  
   listen 80;
   server_name www.domain.ru;
.....
}

After making changes to the Nginx configuration file, you need to restart the web server.

service nginx restart  

Redirect 301 from http to https

For Nginx you need to create two server sections in the configuration file, one for http (80 port) and one for https (443 port).

For the new domain in the nginx config file
Server section, for http discovery (80 port) and redirection settings:

server {  
    server IP listen:80;
    server_name www.domain.ru;
    rewrite ^ https://www.domain.ru$request_uri? 
}

The server section, for opening over https(443 port) where the main domain settings are located.

server {  
    server IP listen:443;
    server_name www.domain.ru;
.....
}

For an existing domain in the nginx conf file

If you are making changes to an existing section of an nginx conf file, do so as follows: From the main domain section, delete the line like

     server IP listen:80;

And create a new server section like this:

server {  
    server IP listen:80;
    server_name www.domain.ru;
    rewrite ^ https://www.domain.ru$request_uri? 
}

After making changes to the Nginx configuration file, it has to be restarted:

service nginx restart  

If you have an SSL certificate for the domain, you can configure the https protocol. Then for the 301 redirect you need to add the following code to the nginx configuration file for the domain:

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

Or

server {  
    #...
    listen server_ip:80;
    server_name www.domain.ru;
    rewrite ^ https://www.domain.ru$request_uri?
}

If the URL of the page has changed, it's better to do a 301 redirect to the new URL:

server {  
    #...
    if ( $request_filename ~ oldpage/ ) {
        rewrite ^ http://www.domain.ru/newpage/?
    }
    #...
}

301 redirect for folder

server {  
    #...
    if ( $request_filename ~ oldfolder/.+ ) {
        rewrite ^(.*) http://www.domain.ru/newfolder/$1
    }
    #...
}

301 redirect from one domain to another

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

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

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

Remove / at the end of the page

location ~ .+/$ {  
rewrite (.+)/$ $1 permanent;  
}

Redirects from pages with index.php

if ($request_uri ~* "^(.*/)index\.php$") {  
        return 301 $1;
    }

Redirects from pages //

merge_slashes off;  
# replace merge_slashes' behavior with "redirect_slashes"
location ~* "//" {  
    rewrite ^(.*)//(.*)$ $1/$2;
    { rewrite ^ $uri permanent;
}

After making changes to the Nginx configuration file, you need to restart the web server.

service nginx restart  
Updated Dec. 10, 2024