如果存在特定参数,则重写URL

时间:2017-07-09 17:18:34

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

我有一个这样的网址:website.co.uk/search.php?a=category&b=test&c=test

我想将search.php重写为/ search /和变量' a'只有它存在。

这样网址会重定向到:website.co.uk/search/category/?b=test&c=test

如果网址为:website.co.uk/search.php?b=test&c=test

然后,这应该重定向到website.co.uk/search/?b=test&c=test

目前我正在使用:

RewriteEngine on
RewriteBase /    

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^search/(.*?)/?$ search.php?a=$1 [L,QSA]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /search\.php\?a=([^\&\ ]+)
RewriteRule ^/?search\.php$ /search/%1? [L,R=301]

如果我输入网址website.co.uk/search.php?a=category&b=test&c=test

重定向到website.co.uk/search/category

如何将其余参数添加到此网址的末尾?

看起来像website.co.uk/search/category/?b=test&c=test

1 个答案:

答案 0 :(得分:0)

您可以在站点根目录中使用这些规则.htaccess:

Options -MultiViews
RewriteEngine on

RewriteCond %{THE_REQUEST} /search\.php\?a=([^&\s]*)\sHTTP [NC]
RewriteRule ^ /search/%1/? [L,NE,R=301]

RewriteCond %{THE_REQUEST} /search\.php(?:\?a=([^&\s]*)&(\S+))?\sHTTP [NC]
RewriteRule ^ /search/%1/?%2 [L,NE,R=301]

RewriteCond %{THE_REQUEST} /search\.php(?:\?(\S+))?\sHTTP [NC]
RewriteRule ^ /search/?%1 [L,NE,R=301]

RewriteRule ^search/?$ search.php [L,NC]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^search/([\w-]+)/?$ search.php?a=$1 [L,NC,QSA]