我尝试过多种方法尝试重定向根上带有查询字符串的URL,例如,如果我试图匹配http://example.com/?bloginfo=racing
Redirect 301 "/?bloginfo=racing" http://example.com/racing或
RedirectMatch 301 ^/?bloginfo=racing$ http://example.com/racing这种情况永远也比不上。在我的.htaccess文件中有一个很好的方法来编写这种重定向吗?
发布于 2017-08-21 13:59:21
如果要匹配查询字符串,则需要使用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。或者,在Apache 2.4+上使用2.4+标志。
将302 (临时)更改为301 (永久),如果这是永久的,并且只有当您确定它工作正常时(以避免缓存问题)。
为了使这更通用,并将/?bloginfo=<something>重定向到/<something>,您可以执行以下操作:
RewriteCond %{QUERY_STRING} ^bloginfo=([^&]+)
RewriteRule ^$ /%1? [R=302,L]%1是对上次匹配CondPattern中捕获的子模式的反向引用。
发布于 2017-08-21 13:59:25
查询字符串与请求URI是独立的变量,因此您必须执行如下操作:
RewriteEngine On
RewriteCond %{QUERY_STRING} bloginfo=racing
RewriteRule ^$ /racing [L,R]https://stackoverflow.com/questions/45798903
复制相似问题