apache对nginx的多次重写条件

时间:2012-09-24 12:24:35

标签: mod-rewrite nginx

任何人都可以帮我吗?

RewriteCond %{REQUEST_URI} !^\/index\.php$
RewriteCond %{REQUEST_URI} !^\/search\/.*
RewriteCond %{REQUEST_URI} ^\/([^/]*)\.php$
RewriteRule (.*) http://www.mydomain.com/index.php?page=$1

我需要将其转换为nginx

我尝试了这个,但它不起作用:

if ($request_uri ~* !^\/search\/.*) {
  if ($request_uri ~* !^\/index\.php$) {
    if ($request_uri ~* ^\/([^/]*)\.php$) {
      rewrite (.*) http://www.mydomain.com/index.php?page=$1;
    }
  }
}

2 个答案:

答案 0 :(得分:1)

我最终使用了:

if ($uri !~ "^/index.php$") {
  set $rule_0 1$rule_0;
}
if ($uri !~ "^/search/.*") {
  set $rule_0 2$rule_0;
}
if ($uri ~ "^/([^/]*).php$") {
  set $rule_0 3$rule_0;
}
if ($rule_0 = "321") {
  rewrite ^(.*) /index.php?page=$1 last;
}

答案 1 :(得分:0)

尝试将~* !更改为!~*,然后合并最后的2:

if ($request_uri !~* ^/search/.*) {
  if ($request_uri !~* ^/index\.php$) {
    rewrite ^/([^/]*)\.php$ http://www.mydomain.com/index.php?page=$1 redirect; 
  }
}

或坚持基于位置的规则:

location ~ /index\.php$ { } 
location ~ /search/.* { } 
location / { 
    rewrite ^/([^/]*)\.php$ http://www.mydomain.com/index.php?page=$1 redirect; 
}