background image

Apache Rewrite 规则:
RewriteRule ^/user/login/$ /user/login.php?login=1&forward=http:

//%{HTTP_HOST} [L]

 
Nginx Rewrite 规则:
rewrite ^/user/login/$ /user/login.php?login=1&forward=http:

//$host last;

Apache 与 Nginx Rewrite 

 

规则的一些功能相同或类似的指令、标记对应关系:

Apache 的 RewriteCond 指令对应 Nginx 的

if

 

指令;

Apache 的 RewriteRule 指令对应 Nginx 的 rewrite

 

指令;

Apache 的[R]标记对应 Nginx 的 redirect

 

标记;

Apache 的[P]标记对应 Nginx 的 last

 

标记;

Apache 的[R,L]标记对应 Nginx 的 redirect

 

标记;

Apache 的[P,L]标记对应 Nginx 的 last

 

标记;

Apache 的[PT,L]标记对应 Nginx 的 last 标记;
允许指定的域名访问本站,其他域名一律跳转到 http:

//www.aaa.com

 

Apache Rewrite 规则:
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

指令不支持嵌套,也不支持 AND、OR 等多条件匹配,相比于 Apache 的

RewriteCond,显得麻烦一些,但是,我们可以通过下一页的 Nginx 配置写法来实现这个

 

示例:
Nginx Rewrite 

 

规则:

if

 (

$host

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

{
set 

$var_wupin_city

 

$1

;

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′)

{