Use Nginx to achieve 301 to jump to the root domain name of https

Based on SEO and security considerations, a 301 jump is required. The following uses Nginx for general processing

Achieve results

You need to jump all the following addresses to the root domain name of https https://chanvinxiao.com

The difference between 301 and 302

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

  • 301: Search engine will transfer weight and PR value
  • 302: No additional processing by search engines

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

http jump to https

The easiest way is to directly return a redirected address in sever, plus 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 belong to the instructions of Nginx's rewrite module, because there is no need to modify the path here, so it is more convenient to use return
  • Both $ host and $ request_uri are embedded variables of the Nginx http module. The combination of the two variables is equivalent to the result of removing the requested http: //

www Jump to the root domain name

This only needs to be processed in https, because all http jumps to https

server {
  listen 443 ssl;
  server_name ~^(?<www>www\.)?(.+)$;
  if ( $www ) {
    return 301 https://$2$request_uri;
  }
...
  • Here , the regular matching function of server_name is used. It can be enabled by adding ~ before its value, and supports PCRE syntax
  • The use of regularization is to confirm whether there is the prefix www. And the capture root domain name, and generate two variables, one is the named capture variable $ www and the other is the numeric capture variable $ 2
  • if the order which does not support the use of variable capture, otherwise it will error ( unknown "1" variable), so increasing the ?<www>value of $ 1 is assigned to the $ www

Reduce the number of jumps

The above settings have satisfied the implementation results, but there is a flaw, that is, http://www.chanvinxiao.com will jump to https://www.chanvinxiao.com first , then jump to https://chanvinxiao.com , It's definitely not as good to perform a second jump as only one jump, so it is best to make it directly in one step, modify the http configuration as follows:

server {
  listen 80;
  server_name ~^(?:www\.)?(.+)$;
  return 301 https://$1$request_uri;
}
  • In the server corresponding to http, change the server_name to regular mode, and replace $ host with the captured root domain name $ 1
  • www will be discarded directly here, so no need to capture, use ?: Mark to achieve only grouping without capturing, so the root domain name behind becomes $ 1
  • The result of this is that regardless of whether or not you have www, you will be redirected to the https root domain name without www.

to sum up

There is no need to specify a specific domain name in the above configuration, which is convenient for compatibility and portability. The following features of Nginx are used:

  • regular match for server_name
  • The return instruction receives the status code and address
  • $ host and $ request_uri embedded variables

Guess you like

Origin www.cnblogs.com/chanvin/p/301-to-root-with-https-by-nginx.html
Recommended