"Nginx Learning" URL address complete final/(backslash)

Source: Internet
Author: User

Problem Description

Connect the prefix name URL to the address: http://127.0.0.1/zt/n4/, change to: http://127.0.0.1/zt/n4/

Why did you do that?

When a Web server receives a URL request with no slash at the end, for example: Http://www.360shouji.com/product, the server searches for a file named "Product" under the root directory of the Web site. If you do not treat product as a directory, then return to the default home page in the ABC directory. When a Web server receives a request with a trailing slash at the end, it is processed directly as a directory. For semantic clarity. Of course, many applications are now route rewriting paths.
 
Configuration Method

Add the following code to nginx.conf in the Nginx configuration file

if (-D $request _filename) {
    rewrite ^/(. *) ([^/]) $ http://$host/$1$2/permanent;
}

In this way, Nginx will be judged, if the request is a folder, will automatically add/symbol at the end, if the request is a file, it will not change the original URL
Here's an explanation for this piece of code:

1,if (-D $request _filename), true if the request is a folder, to execute in the IF statement

2,rewrite is a URL rewrite operation

3,^/(.) ([^/]) $ represents a string that starts with a/symbol and is immediately followed by any character, not at the end of/, in my URL, (.) Furu:/abc/cde, ([^/]) that represents a string that is not ending with/

4,http://$host/$1$2/of the rewritten address, $host is the requested domain name, which is the first parenthesis in front, in my URL is a string of/ABC/CDE, and $ is the second in front of the parentheses, in my URL is

5,permanent says 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.