我尝试了多种方法来尝试使用根上的查询字符串重定向某些 URL,例如,如果我尝试匹配 URL http://example.com/?bloginfo=racing
Redirect 301 "/?bloginfo=racing" http://example.com/racing
或
RedirectMatch 301 ^/?bloginfo=racing$ http://example.com/racing
条件永远不会匹配。在我的 .htaccess
文件中是否有一个好的方法来编写这种重定向?
最佳答案
如果你想匹配查询字符串,你需要使用 mod_rewrite 并检查 RewriteCond
中的 QUERY_STRING 服务器变量。指示。 mod_alias 指令(即 Redirect
和 RedirectMatch
仅匹配 URL 路径,而不匹配查询字符串)。
例如,重定向http://example.com/?bloginfo=racing
至http://example.com/racing
你可以执行如下操作:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^bloginfo=racing$
RewriteRule ^$ /racing? [R=302,L]
尾随?
为了从请求中删除查询字符串,需要进行替换,否则,它将传递到目标 URL。或者,使用 QSD
Apache 2.4+ 上的标志
如果希望将 302(临时)更改为 301(永久),并且仅当您确定其工作正常(以避免缓存问题)时,才将其更改为 301(永久)。
为了使其更通用并重定向 /?bloginfo=<something>
至/<something>
那么你可以执行如下操作:
RewriteCond %{QUERY_STRING} ^bloginfo=([^&]+)
RewriteRule ^$ /%1? [R=302,L]
%1
是对上一次匹配中捕获的子模式的反向引用CondPattern。
关于apache - 301 重定向根查询字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45798903/