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

htaccess mod_rewrite two query string parameters to path format

我要怎么改

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= [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

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

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(top)(?:/([^/])+)?/?$ /?sort=&time= [L]

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