【问题标题】:URL Rewrite - Multiple queryURL 重写 - 多个查询
【发布时间】:2021-12-17 23:53:13
【问题描述】:

我正在尝试了解如何实现以下重写规则。

来自

https://localhost/site/?page=place&place=west
https://localhost/site/?page=location&location=cityname

https://localhost/site/place/west
https://localhost/site/location/city

我可以改变 https://localhost/site/?page=place 到 https://localhost/site/place 但不是上面提到的另一个附加查询。

htaccess

 RewriteEngine On
 #Redirect /site/?page=foobar to /site/foobar
RewriteCond %{THE_REQUEST} /site/(?:index\.php)?\?page=(.+)\sHTTP [NC]
RewriteRule ^ /site/%1? [L,R]
# Internally rewrite new path to the original one
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(?:site/)?(.+)/?$ /site/?page=$1 [L,QSA]

上面的 htaccess 适用于我也需要的以下内容。

https://localhost/site/place
https://localhost/site/about
https://localhost/site/contact

【问题讨论】:

    标签: apache .htaccess mod-rewrite friendly-url url-rewrite-module


    【解决方案1】:

    根据您显示的尝试,请尝试遵循 htaccess 规则文件。确保在测试 URL 之前清除浏览器缓存。新规则与您现有的规则合并。

    RewriteEngine ON
    RewriteBase /site/
    ##New rules from here......
    RewriteCond %{THE_REQUEST} \s/site/?\?page=([^&]*)&place=([^&]*)\s [NC]
    RewriteRule ^ /site/%1/%2? [R=301,L]
    
    # Internally rewrite new path to the original one
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^site/([^/]*)/(.*)/?$ index.php?page=$1&location=$2 [NC,QSA,L]
    

    【讨论】:

    • 不应该 &place= 也遵循 ([^&]*) 规则,因为不能保证这将是未来的最终查询字符串参数
    • 但是在 https://localhost/site/?page=place&place=west&home=yes 上使用 "\S+" 意味着 %2 = "west&home=yes" ?
    • @Martin,是的,我现在已经按照建议编辑了我的解决方案。谢谢。
    • @OneNation,当然,您能否检查一下我编辑的代码,如果这对您有帮助,请告诉我?谢谢。
    • @RavinderSingh13 它不起作用:(我尝试了 location/cityname - include_once(page/location/cityname.php) 无法打开流:没有这样的文件。
    猜你喜欢
    • 2011-12-07
    • 1970-01-01
    • 1970-01-01
    • 2011-02-12
    • 2012-10-22
    • 1970-01-01
    • 1970-01-01
    • 2017-05-18
    • 2021-06-25
    相关资源
    最近更新 更多