htaccess 文件排除机器人,但允许它们访问 robots.txt

问题描述 投票:0回答:1

我正在尝试使用 htaccess 阻止特定的用户代理,但允许他们查看 robots.txt 我认为以下内容可行,但他们仍然收到 robots.txt 的 308 错误 大家有什么想法吗?

 RewriteCond %{HTTP_USER_AGENT} bot1 [NC,OR]
 RewriteCond %{HTTP_USER_AGENT} bot2 [NC,OR]
 RewriteCond %{HTTP_USER_AGENT} bot3 [NC]
 RewriteCond %{REQUEST_URI} !(/)?robots\.txt
 RewriteRule ^(.*)$ http://%{REMOTE_ADDR}/$1 [R=308,L]
.htaccess
1个回答
0
投票

如果您想阻止特定的用户代理但允许他们访问 robots.txt ,您应该考虑使用 F 标志来禁止。这是代码的更新版本:

RewriteEngine On

# Allow access to robots.txt for all user agents
RewriteRule ^robots\.txt$ - [L]

# Block specific user agents
RewriteCond %{HTTP_USER_AGENT} bot1 [NC,OR]
RewriteCond %{HTTP_USER_AGENT} bot2 [NC,OR]
RewriteCond %{HTTP_USER_AGENT} bot3 [NC]
RewriteRule ^ - [F]
© www.soinside.com 2019 - 2024. All rights reserved.