On the difference _php skill of rewrite between Apache and Nginx

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. Comparison of Nginx and Apache rewrite rule examples

Simple Nginx and Apache rewrite rules are not very different and can be completely 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's easy to see that Apache's rewrite rule changes to Nginx's rewrite rule is quite simple, if you change the rules, use the "nginx-t" command to check that the nginx.conf configuration file has syntax errors, you can try to enclose the condition with quotes. For example, the Nginx rewrite rule will report syntax errors:

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

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 permutation string of the Nginx Rewrite rule, which is required in Nginx.

In addition, the rewrite rules for Apache and Nginx also differ in terms of 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 has some of the same or similar instructions, tag correspondences as Nginx Rewrite rules:
The rewritecond instruction of Apache corresponds to the if instruction of nginx;
Apache's Rewriterule instruction corresponds to Nginx's rewrite instruction;
The [R] tag of Apache corresponds to the redirect mark of the Nginx;
The [P] tag of Apache corresponds to the last tag of the Nginx;
The [r,l] tag of Apache corresponds to the redirect tag of the Nginx;
The [p,l] tag of Apache corresponds to the last mark of the Nginx;
The [pt,l] tag of Apache corresponds to the last mark of the Nginx;

Allow the designated domain name access to the site, the other domain name jump 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]

The nginx if directive does not support nesting, and does not support multiple conditional matches such as and, or, but it is a bit more cumbersome than Apache Rewritecond, but we can do this by following the Nginx configuration of the next page:
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;

}

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.