【问题标题】:transfer rewrite rule from apache to nginx将重写规则从 apache 转移到 nginx
【发布时间】:2016-10-13 16:43:01
【问题描述】:

我在将 sto 从 apache 主机转移到 nginx 主机的两次重写时遇到问题。我已经尝试在线阅读,这是我想出的最好的,不幸的是我在 nginx 上几乎没有使用 regexp 和重写,请帮助。

apache重写

RewriteRule ^news/([^/]+)/([^/]+)/([^/]+)\.html$    news_dett.php?l=$1&par=$2&titolo=$3 [L]

结果阿帕奇

exampleSite/news/it/44/Title_Of_the_news.html

apache重写

RewriteRule ^Research-and-develop\.html$     researchdevelop?l=$1 [L]

结果阿帕奇

exampleSite/Research-and-develop.html

nginx 重写

location /news {
     rewrite ^/news http://$http_host/news_dett.php?l=$1&par=$2&titolo=$3 redirect;
    }

结果 nginx

点击链接exampleSite/news/it/44/Title_Of_the_news.html我们去 exampleSite/news_dett.php?l=&par=&titolo=

事实上这根本行不通

nginx 重写

location /Research {
            rewrite ^/Research-and-develop\.html$ http://$http_host/researchdevelop?l=$1 redirect;
    }

结果 nginx

点击链接exampleSite/Research-and-develop.html 我们去exampleSite/researchdevelop.php?l=

你能帮我为 nginx 设置正确的重写模式吗?

【问题讨论】:

    标签: apache nginx url-rewriting


    【解决方案1】:

    您的/news 重写缺少$1$2 的捕获。重定向到同一个服务器时,方案和服务器部分是不必要的。此外,last 执行内部重定向,从而避免将重写暴露给客户。它应该是这样的:

    rewrite ^/news/([^/]+)/([^/]+)/([^/]+)\.html$ /news_dett.php?l=$1&par=$2&titolo=$3 last;
    

    可选择放置在location /news { ... } 块内。

    您的问题尚不清楚第二个重写规则中$1 的值(我假设它是空的)。完全匹配 rewrite 指令的替代方法是完全匹配位置:

    location = /Research-and-develop.html {
        rewrite ^ /researchdevelop?l= last;
    }
    

    另请参阅rewrite documentationlocation documentaion 和有用的regular expression resource

    【讨论】:

      猜你喜欢
      • 2014-06-30
      • 2016-08-09
      • 2014-11-17
      • 2022-11-20
      • 2016-06-18
      • 1970-01-01
      • 1970-01-01
      • 2017-07-26
      相关资源
      最近更新 更多