【问题标题】:htaccess rewrite if user requests for top level domain如果用户请求顶级域,则 htaccess 重写
【发布时间】:2026-01-07 20:00:01
【问题描述】:

目前我有以下规则:
- 如果用户是 Yandexbot 或 bingbot,则为除 index.html 之外的所有页面提供 403 错误

RewriteCond %{HTTP_USER_AGENT} YandexBot|bingbot [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(index\.html) [NC]
#  RewriteRule ^(.*)$ /index.html? [L]
RewriteRule .* - [F]

但我发现这条规则会阻止这些机器人访问http://example.com。当机器人请求这些 URL 时,我将提供没有 301 的“index.html”。

http://example.com
http://example.com/
http://example.com/index.html

但我希望这些机器人不要访问这些 URL。我的意思是域名后面不会有任何字符:

http://mywebsite.com/?aaa
ttp://mywebsite.com/abcd
http://mywebsite.com/1234some

htaccess 文件如何?

【问题讨论】:

  • 嗯,所以它可以访问除.+ 而不是.* 之外的任何内容? (并检查RewriteCond 中的空QUERY_STRING

标签: apache .htaccess mod-rewrite


【解决方案1】:

试试:

RewriteCond %{HTTP_USER_AGENT} YandexBot|bingbot [NC]
RewriteCond %{THE_REQUEST} !\ /+index\.html(\ |$)
RewriteCond %{THE_REQUEST} \ /+[^\ ]+
RewriteRule ^ - [L,F]

所以:

  • 用户代理中包含“YandexBot”或“bingbot”的任何内容
  • 如果请求不是针对/index.html
  • 如果请求不仅仅是/

【讨论】: