Nginx rewrite instance-Add/symbol at the end of the URL

Source: Internet
Author: User

Add the following in location in the configuration file:Code

If (-d $ request_filename ){
Rewrite ^/(. *) ([^/]) $ http: // $ host/$1 $2/permanent;
}

in this way, nginx will make a judgment. If a folder is requested, the/symbol will be automatically added at the end. If a file is requested, the original URL is not changed.
The following explains the code:

1, if (-d $ request_filename), if the request is a folder, it is true, and it is executed in the IF statement
2, rewrite is the URL rewriting operation
3, ^ /(. *) ([^/]) $ indicates a string that starts with a slash (/) followed by any character and does not end with a slash (/). In my URL ,(. *) represents wordpres, ([^/]) represents S
4, http: // $ host/$1 $2/Represents the rewritten address, $ host is the requested domain name, $1 is the content in the first bracket, and in my URL, wordpres $2 is the content in the second bracket, in my URL is S
5, permanent indicates, the return value is 301

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.