【问题标题】:htaccess mod_rewrite two query string parameters to path formathtaccess mod_rewrite 两个查询字符串参数为路径格式
【发布时间】:2016-03-04 12:00:12
【问题描述】:

我将如何改变

http://example.com/?sort=top&time=variable

到

http://example.com/top/variable

&time=variable 可能并不总是被使用。

我当前的 htaccess:

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?showcase/(.*?)/?$ /showcase.php?id=$1 [L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /showcase\.php\?id=([^\&\ ]+)
RewriteRule ^/?showcase\.php$ /showcase/%1? [L,R=301]

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [QSA,L]

将http://example.com/showcase.php?id=int 重写为http://example.com/showcase/int。

【问题讨论】:

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


    【解决方案1】:

    如果代码在.htaccess 文件中,请使用^ 而不是^/?。对于您的特定情况,您可以修改/添加到现有重定向,如下所示:

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(top)(?:/([^/])+)?/?$ /?sort=$1&time=$2 [L]
    
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /\?sort=(top)(?:&time=([^\s&]+))? [NC]
    RewriteRule ^ /%1/%2? [R=301,L]
    

    【讨论】:

    • 行得通!非常感谢。老实说,我无法专心写这些东西,它似乎太复杂了
    猜你喜欢
    • 2016-03-26
    • 1970-01-01
    • 1970-01-01
    • 2021-05-15
    • 2010-12-25
    • 2011-04-01
    • 2015-06-12
    • 1970-01-01
    • 2011-03-07
    相关资源
    最近更新 更多