【问题标题】:apache multiple rewrite conditions to nginxapache多次重写条件到nginx
【发布时间】:2012-09-15 21:50:47
【问题描述】:

谁能帮我解决这个问题?

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;
    }
  }
}

【问题讨论】:

    标签: mod-rewrite nginx


    【解决方案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;
    }
    

    【讨论】:

      【解决方案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; 
      }
      

      【讨论】:

      • 第一个不行,因为nginx不接受嵌套。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-10
      • 2017-01-18
      • 1970-01-01
      • 2012-02-25
      • 1970-01-01
      相关资源
      最近更新 更多