htaccess mod_rewrite两个查询字符串参数为路径格式

时间:2016-03-04 03:52:02

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

我将如何更改

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

1 个答案:

答案 0 :(得分: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]