Use Nginx to implement 301 jump to https's root domain example code

  • 2020-05-24 06:48:17
  • OfStack

For SEO and security reasons, a 301 jump is required. Nginx is used for general processing

Achieve results

The following addresses need to be redirected to https:// chanvinxiao.com

http:// chanvinxiao.com (http without www) http: / / www. chanvinxiao. com (with www http) https: / / www. chanvinxiao. com (with www https)

The difference between 301 and 302

301 is a permanent redirect and 302 is a temporary jump, the main difference being how search engines treat this

301: the search engine will transfer the weight and PR value 302: search engines do not perform additional processing

Now the hope is that the search engine thinks the original address no longer exists, and completely moves to the new address, so use 301

http jump to https

The easiest way to do this is to simply return a redirected address in sever with a 301 status code in the middle (otherwise the default is 302)


server {
 listen 80;
 return 301 https://$host$request_uri;
}
Both return and rewrite are part of Nginx's rewrite module directive, and since there is no need to modify the path, return is more convenient $host and $request_uri are both embedded variables in the Nginx http module, and the combination of the two variables is equivalent to the result of removing the requested http://

www jumps to the root domain

This only needs to be handled in https, because all http has jumped to https


server {
 listen 443 ssl;
 server_name ~^(?<www>www\.)?(.+)$;
 if ( $www ) {
 return 301 https://$2$request_uri;
 }
...
This takes advantage of the regular matching function of server_name, which can be enabled by adding ~ before its value, and supports the PCRE syntax The regular is used to verify that there is a prefix www. And to capture the root domain name, generating two variables, one for the named capture variable $www and one for the numeric capture variable $2 if does not support the use of sequence to capture variables, otherwise an error will be reported (unknown "1" variable), so add ? < www > The value of $1 is assigned to $www

Reduce the number of jumps

The above Settings has met the implementation results, but there is 1 point defects, is http: / / www chanvinxiao. com will jump to https: / / www chanvinxiao. com, then jump to https: / / chanvinxiao com, for two jump certainly is not just jump one, so it's best to let the direct step 1, modify http configuration is as follows:


server {
 listen 80;
 server_name ~^(?:www\.)?(.+)$;
 return 301 https://$1$request_uri;
}

In sever corresponding to http, server_name is also changed to regular mode, and $host is replaced with the captured root domain $1
www is simply discarded here, so you don't need to capture it, use ? : indicates that the implementation only groups and does not capture, so the following root domain becomes $1
The result is that the https root domain jumps to www root domain without www, whether or not it originally has www or not

conclusion

There is no need to specify a specific domain name in the above configuration, which can be easily compatible and portable. The following features of Nginx are used:

Regular matching of server_name The return instruction receives the status code and address $host and $request_uri embedded variables

Related articles: