Google 正在访问带有参数的页面,我需要阻止它。
在所有带有参数的页面上给出页面404 看起来像 site.com?q=text 或 site.com/?q=text
但如果链接只是 site.com 则不会阻止
我为 .htaccess 编写了这个脚本
ErrorDocument 403 "Your connection was rejected"
ErrorDocument 404 /404.shtml
RewriteEngine On
#RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_USER_AGENT} (Googlebot) [NC]
RewriteCond %{REQUEST_URI} ^/q= [NC]
RewriteRule ^ - [F,L]
但是有两个问题 首先 - 如何设置参数
第二个 - 当他们阻止不显示 404 页面并显示时
Not Found
The requested URL was not found on this server.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
但是我给出了ErrorDocument 404 /404.shtml。 为什么apache找不到404.shtml? 如果我给出缺失的页面,它会正常显示404.shtml。
首先,您需要使用
QUERY_STRING
而不是 REQUEST_URI
来匹配查询字符串。
此外,您收到此错误是因为查询字符串未进入重定向的 URL,即
/404.shtml?q=text
重定向后的 404
,并且您的规则将再次尝试重定向到相同的 URL。
理想情况下,您应该像这样禁止返回
403
:
RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} (Googlebot) [NC]
RewriteCond %{QUERY_STRING} ^q= [NC]
RewriteRule ^ - [F]
但是,如果您必须使用
404
,那么只能像这样使用它:
RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} (Googlebot) [NC]
RewriteCond %{QUERY_STRING} ^q= [NC]
RewriteRule !^404\.shtml$ - [R=404,NC,L]
它将对除
/404.shtml
之外的所有 URL 执行此规则。
您还可以像这样检查
REDIRECT_STATUS
:
RewriteEngine On
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_USER_AGENT} (Googlebot) [NC]
RewriteCond %{QUERY_STRING} ^q= [NC]
RewriteRule ^ - [R=404,L]
这将仅对原始 URL 执行此规则。