【问题标题】:Converting Apache htaccess rewrite condition to nginx将 Apache htaccess 重写条件转换为 nginx
【发布时间】:2012-02-25 03:06:23
【问题描述】:

我正在尝试移植此 Apache Rewrite 规则以在 nginx Web 服务器上使用。

RewriteBase /srv/www/htdocs
RewriteCond %{REQUEST_URI} ^/(files.*)$
RewriteRule ^(.*)$ %1 [L]

我不知道如何在 nginx.conf 中解决这个问题。 有人可以帮忙吗?

谢谢,帕特里斯

【问题讨论】:

  • 只是好奇;规则应该做什么?因为我看它的方式,它将/files.example之类的url重写为/files/example,这显然是无用的。

标签: .htaccess mod-rewrite url-rewriting nginx rewrite


【解决方案1】:

试试这个:

if ($uri ~ "^/(files.*)$"){
    set $rule_0 1$rule_0;
    set $bref_1 $1;
}
if ($rule_0 = "1"){
    rewrite ^/(.*)$ /$bref_1 last;
}

来源:http://www.anilcetin.com/convert-apache-htaccess-to-nginx/

【讨论】:

    【解决方案2】:

    可能更简单:

    if ( $uri ~ "^/(files.*)$") {
         rewrite ^/(.*)$ /$1 last;
    }
    

    【讨论】:

      【解决方案3】:

      感谢您的快速回答!最后,我解决了这个问题,类似于 Sergeis 的建议,但嵌套了一个条件。

      location / {
          root   /srv/www/htdocs;
      
          # all under "/files/" breaks further processing
          location ~ ^/files(.*)$ {
              break;
          }
      
          # rewrite rules for all paths not under "/files/"
          if (!-e $request_filename) {
              rewrite ^/([^/\.]+)((\/?)(.*?))$ /process.php?id=$1&path=$2 last;
          }
      
      }
      

      【讨论】:

        猜你喜欢
        • 2014-11-10
        • 1970-01-01
        • 2014-06-30
        • 2016-08-09
        • 2017-06-06
        • 2011-04-16
        • 2021-04-04
        • 2011-08-21
        • 2016-01-16
        相关资源
        最近更新 更多