Correct implementation of nginx 301 redirection

Source: Internet
Author: User
Tags regular expression

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.