我尝试使用 .htaccess 进行 301 重定向。
问题:
/?view=products&id=12345 -> /8831
旧地址和新地址之间没有任何关系。
由于某种原因
Redirect 301 /?view=products&id=12345 /8831
不起作用。如果我删除问号,它就可以在没有问号的情况下工作。
我也尝试过:
RewriteCond %{QUERY_STRING} view=products&id=12345
RewriteRule .*$ /8831 [L,R=301]
但它会将我重定向到 /8831?view=products&id=12345
,这对我来说不好。我不需要新网址中的查询字符串-
最佳答案
RewriteCond %{QUERY_STRING} view=products&id=12345
RewriteRule .*$ /8831? [L,R=301]
结尾的 ?
将阻止附加原始查询参数,除非您再次给出 [QSA]
标志。
来自the manual :
Note: Query String The Pattern will not be matched against the query string. Instead, you must use a RewriteCond with the %{QUERY_STRING} variable. You can, however, create URLs in the substitution string, containing a query string part. Simply use a question mark inside the substitution string, to indicate that the following text should be re-injected into the query string. When you want to erase an existing query string, end the substitution string with just a question mark. To combine a new query string with an old one, use the [QSA] flag.
关于regex - 使用 RewriteCond %{QUERY_STRING} 而不将查询字符串附加到新地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15277182/