Method for HTTP 301 to jump to a domain name with www in the Nginx server

  • 2020-05-09 19:57:08
  • OfStack

From nginx's official documentation document, the correct nginx https 301 jumps to the www domain name method as follows:

HTTP 301 jumps to the domain name method with www

     

 server {
        listen       80;
        server_name  example.org;
        return       301 http://www.example.org$request_uri;
    }     server {
        listen       80;
        server_name  www.example.org;
        ...
    }   

HTTPS 301 jumps to the domain name method with www

 

   server {
            listen 80;
            server_name www.domain.com;
            // $scheme will get the http protocol
            // and 301 is best practice for tablet, phone, desktop and seo
            return 301 $scheme://domain.com$request_uri;
    }
    
    server {
            listen 80;
            server_name domain.com;
            // here goes the rest of your config file
            // example
            location / {
    
                rewrite ^/cp/login?$ /cp/login.php last;
                // etc etc...
    
            }
    }

       

Check the version of nginx you are using with the nginx-v   command first. Here's how to jump from nginx301 to www with the www.ksharpdabu.info to   ksharpdabu.info for the older version

     

server {
        server_name  www.domain.com;
        rewrite ^(.*) http://domain.com$1 permanent;
    }     server {
        server_name  domain.com;
        #The rest of your configuration goes here#
    }


So you need two server segments.

Jump from ksharpdabu.info to   www.ksharpdabu.info

     

server {
        server_name  domain.com;
        rewrite ^(.*) http://www.domain.com$1 permanent;
    }     server {
        server_name  www.domain.com;
        #The rest of your configuration goes here#
    }


After the above Settings, jump to the specified domain name with the method of rewrite, which is conducive to SEO
Here is my example of the nginx configuration from www.google.com to google.com:

     

server {
        server_name  www.google.com;
        rewrite ^(.*) http://google.com$1 permanent;
    }
    server {
           listen 80;
           server_name google.com;
           index index.php index.html;
           ####
           # now pull the site from one directory #
           root /var/www/www.google.com/web;
           # done #
           location = /favicon.ico {
                    log_not_found off;
                    access_log off;
           }
    }


There is another way to avoid rewirte online, as follows:

     

server {
        #listen 80 is default
        server_name www.example.com;
        return 301 $scheme://example.com$request_uri;
    }     server {
        #listen 80 is default
        server_name example.com;
        ## here goes the rest of your conf...
    }


Because return can be used in all versions, while rewrite can cause 301 errors depending on the version. And you can simply stop performing matches and searches.

http and https are included below. Same server.

     

server {
        listen 80;
        listen 443 ssl;
        server_name www.example.com;
        return 301 $scheme://example.com$request_uri;
    }     server {
        listen 80;
        listen 443 ssl;
        server_name example.com;
        # rest goes here...
    }

The $scheme variable will only contain http if your server only listens on port 80 (the default is port 80) and the ssl keyword is not included in the option to listen at the same time. Without this variable, you will not get the jump results you want.

Force all http to jump to https, SSL (personal config on UNIX with IPv4, IPv6, SPDY...) :

#
# Redirect all www to non-www
#
server {
    server_name          www.example.com;
    ssl_certificate      ssl/example.com/crt;
    ssl_certificate_key  ssl/example.com/key;
    listen               *:80;
    listen               *:443 ssl spdy;
    listen               [::]:80 ipv6only=on;
    listen               [::]:443 ssl spdy ipv6only=on;     return 301 https://example.com$request_uri;
} #
# Redirect all non-encrypted to encrypted
#
server {
    server_name          example.com;
    listen               *:80;
    listen               [::]:80;     return 301 https://example.com$request_uri;
} #
# There we go!
#
server {
    server_name          example.com;
    ssl_certificate      ssl/example.com/crt;
    ssl_certificate_key  ssl/example.com/key;
    listen               *:443 ssl spdy;
    listen               [::]:443 ssl spdy;     # rest goes here...
}
 
#
# Redirect all www to non-www
#
server {
    server_name          www.example.com;
    ssl_certificate      ssl/example.com/crt;
    ssl_certificate_key  ssl/example.com/key;
    listen               *:80;
    listen               *:443 ssl spdy;
    listen               [::]:80 ipv6only=on;
    listen               [::]:443 ssl spdy ipv6only=on;
 
    return 301 https://example.com$request_uri;
}
 
#
# Redirect all non-encrypted to encrypted
#
server {
    server_name          example.com;
    listen               *:80;
    listen               [::]:80;
 
    return 301 https://example.com$request_uri;
}
 
#
# There we go!
#
server {
    server_name          example.com;
    ssl_certificate      ssl/example.com/crt;
    ssl_certificate_key  ssl/example.com/key;
    listen               *:443 ssl spdy;
    listen               [::]:443 ssl spdy;
 
    # rest goes here...
}

 


Related articles: