Nginx redirection rules
Rewrite command
Nginx rewrite is equivalent to apache rewriterule (in most cases, the original apache rewrite rules can be directly used with quotation marks). It can be used in server, location, and IF condition judgment blocks, the command format is as follows:
Replace the target flag with the rewrite regular expression
The flag can be in the following formats:
Last-this Flag is basically used.
Break-abort Rewirte and do not continue matching
Redirect-return the HTTP status 302 of the temporary redirect
Permanent-returns the HTTP status 301 for permanent redirection
Detailed steps for setting 301 redirection in nginx: Enable and modify
The code is as follows: |
Copy code |
Server { Listen 80; Server_name www.111cn.net 111cn.net; If ($ host! = 'Www .111cn.net '){ Rewrite ^/(. *) $ http://www.111cn.net $1 permanent; } } |
Note:
The code is as follows: |
Copy code |
If ($ host! = 'Www .111cn.net '){ Rewrite ^/(. *) $ http://www.111cn.net $1 permanent; } |
Use esc to exit editing mode after adding
: Wq is saved directly.
Then run
The code is as follows: |
Copy code |
[Root @ localhost ~] #/Etc/init. d/nginx restart |
Last-this Flag is basically used.
Break-abort Rewirte and do not continue matching
Redirect-return the HTTP status 302 of the temporary redirect
Permanent-returns the HTTP status 301 for permanent redirection