大网数据广告活动
网站运营与故障问答

Block blank user-agents bots but make exception for ip address possible?

· 52CE 我爱测

I have the following in my .htaccess file on website2, but when my other website on website1 makes requests to website2 it is blocked because the server user-agent is blank. Is there are a way to allow a single IP address into th…

问题原文

原帖链接 · seefah · CC BY-SA 4.0

I have the following in my .htaccess file on website2, but when my other website on website1 makes requests to website2 it is blocked because the server user-agent is blank.

Is there are a way to allow a single IP address into the code below, or some other method to block the empty user-agent bots but allow my website1 to make requests to files on website2?

SetEnvIfNoCase User-Agent ^$ bad_bot #leave this for blank user-agents
SetEnvIfNoCase User-Agent "^more-bad-bots" bad_bot

<Limit GET POST HEAD>
Order Allow,Deny
Allow from all
Deny from env=bad_bot
</Limit>

已采纳解答

原帖链接 · MrWhite · CC BY-SA 4.0

... because the server user-agent is blank.

Why not set a user-agent in your script? You shouldn't be making HTTP requests with blank user-agents. (If using PHP then this may be as simple as calling ini_set('user_agent','my-user-agent'))

Is there a way to allow a single IP address

You can unset the bad_bot environment variable if the requesting IP is known. For example:

# Leave this for blank user-agents
SetEnvIfNoCase User-Agent ^$ bad_bot
SetEnvIfNoCase User-Agent "^more-bad-bots" bad_bot

# Unset bad_bot if request is from 203.0.113.111
SetEnvIf Remote_Addr ^203\.0\.113\.111$ !bad_bot

:

I've also removed your line-end comment - Apache does not support them! That "line-end comment" was setting 5 additional environment variables: #leave, this, for, blank and user-agents!

Apache only supports full-line comments.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Webmasters Stack Exchange
适用范围
以原帖系统、软件版本与发布日期为准(2018-10-15)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。