htaccess Redirect if query string variable present

血红的双手。 提交于 2019-12-11 01:06:25

问题


I need to add style=2 to all urls within a directory ONLY if the url does NOT already contain style=2.

Within the directory root, I have an .htaccess with the following:

RewriteCond %{QUERY_STRING} !(?style=|&style=)
RewriteRule ^(.*)$ %1&style=2? [R=301,QSA]

I know it's wrong. Can anyone help?


回答1:


Change your code to this to make it work:

RewriteCond %{QUERY_STRING} !(^|&)style=2(&|$) [NC]
RewriteRule ^ %{REQUEST_URI}?style=2 [R=301,L,QSA]



回答2:


Try this :

RewriteCond %{QUERY_STRING} !^(.*&)?style=
RewriteRule ^(.*)$ /$1?style=2 [L,QSA,R=301]


来源:https://stackoverflow.com/questions/13066142/htaccess-redirect-if-query-string-variable-present

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!