查询字符串重定向到相同的 url

Query strings redirecting to the one the same url

我尝试使用查询字符串重定向 url 的列表,但是这些 规则无效:
Redirect 301 /tg.php?uid=www567461d4d44a53.12485104 http://example.com/
Redirect 301 /test/?page_id=45 http://example.com/
Redirect 301 /?p=17518 http://example.com/
Redirect 301 /?page_id=5960 http://example.com/

请阅读您使用的工具的文档。 Apache 的 mod_alias 明确指出,如果您需要检查查询字符串以进行重定向而不是别名模块,则应使用重写模块。这意味着您应该使用 RewriteRule 而不是 Redirect 规则:

RewriteEngine on

RewriteCond %{QUERY_STRING} ^uid=www567461d4d44a53.12485104$
RewriteRule ^/?tg\.php$ http://example.com/ [R=301]

RewriteCond %{QUERY_STRING} ^page_id=45$
RewriteRule ^/?test/?$ http://example.com/ [R=301]

RewriteCond %{QUERY_STRING} ^p=17518$
RewriteRule ^/?$ http://example.com/ [R=301]

RewriteCond %{QUERY_STRING} ^page_id=5960$
RewriteRule ^/?$ http://example.com/ [R=301]

一般性评论:您应该始终倾向于将此类规则放在 http 服务器主机配置中,而不是使用动态配置文件 (".htaccess")。那些动态配置文件增加了复杂性,通常是意外行为的原因,难以调试,它们确实减慢了 http 服务器的速度,而且通常是徒劳的,它们可以打开通向安全噩梦的途径。它们仅在您无法访问真正的 http 服务器主机配置的情况下提供(阅读:非常便宜的服务提供商)。