A brief talk on the difference between Apache and Nginx rewrite _php tutorial

Source: Internet
Author: User
1. Nginx Rewrite rules related directives

Nginx rewrite rules related directives have if, rewrite, set, return, break, etc., where rewrite is the most critical instruction. A simple nginx rewrite rule syntax is as follows:

Rewrite ^/b/(. *) \.html/play.php?video=$1 break;

If you add an if statement, the example is as follows:
if (!-f $request _filename)

{Rewrite ^/img/(. *) $/site/$host/images/$1 last;}

2. Nginx and Apache Rewrite rule example comparison

Simple Nginx and Apache rewrite rules are not very different, basically can be fully compatible.


Apache Rewrite Rules:

Rewriterule ^/(mianshi|xianjing)/$/zl/index.php?name=$1 [L]

Rewriterule ^/ceshi/$/zl/ceshi.php [L]

Rewriterule ^/(Mianshi) _ ([a-za-z]+)/$/zl/index.php?name=$1_$2 [L] Rewriterule ^/PINGCE ([0-9]*]/$/zl/pingce.php?id= $ [L]

Nginx Rewrite Rules:

Rewrite ^/(mianshi|xianjing)/$/zl/index.php?name=$1 last;

Rewrite ^/ceshi/$/zl/ceshi.php last;

Rewrite ^/(Mianshi) _ ([a-za-z]+)/$/zl/index.php?name=$1_$2 last;

Rewrite ^/PINGCE ([0-9]*)/$/zl/pingce.php?id=$1 last;

It is not difficult to find the rewrite rules of Apache to Nginx rewrite rules is very simple, if you change the rules, using the "nginx-t" command to check that the nginx.conf configuration file has a syntax error, you can try to give the condition quotation marks. For example, the Nginx rewrite rule will report a syntax error:

Rewrite ^/([0-9]{5}). html$/x.jsp?id=$1 last; the quotation marks are correct:

Rewrite "^/([0-9]{5}). html$"/x.jsp?id=$1 last;

The rewrite rules for Apache and Nginx have subtle differences in URL jumps:

Apache Rewrite Rules:

Rewriterule ^/html/tagindex/([a-za-z]+)/.*$/$1/[r=301,l]
Nginx Rewrite Rules:

Rewrite ^/html/tagindex/([a-za-z]+)/.*$/http $host/$1/permanent;


In the above example, we notice that the "http://$host" is added to the replacement string of the Nginx Rewrite rule, which is required in Nginx.

In addition, the rewrite rules for Apache and Nginx also differ in variable names, such as:
Apache Rewrite Rules:

Rewriterule ^/user/login/$/user/login.php?login=1&forward=http://%{http_host} [L]

Nginx Rewrite Rules:

Rewrite ^/user/login/$/user/login.php?login=1&forward=http://$host last;
Apache and Nginx Rewrite some functions of the same or similar directive, tag correspondence:
Apache's Rewritecond instruction corresponds to Nginx's if command;
Apache's Rewriterule instruction corresponds to Nginx's rewrite instruction;
Apache's [R] tag corresponds to Nginx's redirect tag;
Apache's [P] tag corresponds to the last tag of Nginx;
Apache's [r,l] tag corresponds to Nginx's redirect tag;
Apache's [p,l] tag corresponds to the last tag of Nginx;
Apache's [pt,l] tag corresponds to the last tag of Nginx;

Allow the specified domain name to access this site, other domain names are redirected to http://www.aaa.com:

Apache Rewrite Rules:

Rewritecond%{http_host} ^ (. *?) \.domain\.com$

Rewritecond%{http_host}!^qita\.domain\.com$ rewritecond%{document_root}/market/%1/index.htm-f

Rewriterule ^/wu/$/market/%1/index.htm [L]

Nginx if directive does not support nesting, also does not support and, or and so on multi-condition match, compared to the Apache Rewritecond, appears to be troublesome, but, we can implement this example through the next page of Nginx configuration notation:
Nginx Rewrite Rules:

if ($host ~* ^ (. *?) \.domain\.com$)

{

Set $var _wupin_city $;

Set $var _wupin ' 1′;

}

if ($host ~* ^qita\.domain\.com$)

{

Set $var _wupin ' 0′;

}

if (!-f $document _root/market/$var _wupin_city/index.htm)

{

Set $var _wupin ' 0′;

}

if ($var _wupin ~ ' 1′)

{

Rewrite ^/wu/$/market/$var _wupin_city/index.htm last;

}

http://www.bkjia.com/PHPjc/326583.html www.bkjia.com true http://www.bkjia.com/PHPjc/326583.html techarticle 1. Nginx Rewrite rules related directives nginx rewrite rules related directives have if, rewrite, set, return, break, etc., where rewrite is the most important instruction. A simple nginx rewrite rule language ...

  • Related Article

    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.