如果存在查询字符串变量,则 htaccess 重定向
问题描述:
仅当 url 尚未包含 style=2
时,我才需要将 style=2
添加到目录中的所有 url.
I need to add style=2
to all urls within a directory ONLY if the url does NOT already contain style=2
.
在根目录中,我有一个 .htaccess 文件,内容如下:
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?
答
将您的代码更改为以下内容以使其工作:
Change your code to this to make it work:
RewriteCond %{QUERY_STRING} !(^|&)style=2(&|$) [NC]
RewriteRule ^ %{REQUEST_URI}?style=2 [R=301,L,QSA]