【问题标题】:Rewrite URL on Site using Nginx使用 Nginx 重写站点上的 URL
【发布时间】:2018-06-16 11:36:16
【问题描述】:

我目前的网址是这样的:

mysite.com/test/?country=uk&number=1234&search=British+Telecom&limit=8

我想变成这样:

mysite.com/test/uk/1234/British-Telecom/8

我对 Apache mod_rewrite 有一定的了解,但从未接触过 Nginx 版本。有人有想法吗?

我发现的其他一点是该网站是 Wordpress,普通页面在重写 url 时可以正常工作,但我的代码位于单独的非 wp 文件夹中。 Apache 不运行时,WP 的 .htaccess 怎么会起作用?

另一个复杂的问题是我的 Varnish 缓存正在工作 - 这会干扰吗?

【问题讨论】:

    标签: php url nginx mod-rewrite rewriting


    【解决方案1】:

    您必须使用([^/]*) 正则表达式捕获get 变量,然后将其与$1,$2 一起使用...

    像这样使用 htaccess:

    RewriteEngine on
    
    RewriteRule ^test/([^/]*)/([^/]*)/([^/]*)/([^/]*)$     ./yourScript.php?country=$1&number=$2&search=$3&limit=$4 [L]
    

    将yourScript.php 更改为您的脚本名称。

    【讨论】:

    • 感谢您的回答,但我确实在我的问题中声明重写是针对 Nginx 而不是 Apache。我确实通过 Apache 到 Nginx 转换器运行了您的代码并得到了这个: location /test { rewrite ^/test/([^/]*)/([^/]*)/([^/]*)/([ ^/]*)$ /index.php?country=$1&number=$2&search=$3&limit=$4 break; } 但它没有用。
    • 我也在运行 Varnish - 这会干扰吗?
    • 它运行的 PS 脚本是 test/index.php
    • @CarlWilson,这样做:RewriteRule ^test/([^/]*)/([^/]*)/([^/]*)/([^/]*)$ test/index.php?country=$1&number=$2&search=$3&limit=$4 [L]
    猜你喜欢
    • 2011-11-19
    • 1970-01-01
    • 2018-02-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-02
    • 1970-01-01
    • 2015-04-28
    • 1970-01-01
    相关资源
    最近更新 更多