禁止空用户代理,但允许它用于某些特定域,通过使用 htaccess
forbidden empty user agent, but allow it for some specific domain, by using htaccess
我想屏蔽空的用户代理,但是在网站的脚本中,有一些片段使用了file_get_contents功能,也会被屏蔽。我尝试将 header 添加到 file_get_contents,但效果不佳。所以我尝试使用htaccess绕过本地主机。
这是 htaccess 中的规则:
#Block empty User-Agents
RewriteCond %{HTTP_USER_AGENT} ^-?$
RewriteCond %{HTTP_HOST} !^http://(.*\.)?example\.com [NC]
RewriteRule ^ - [F]
但是不行。我该如何改变?提前致谢。
经过多次编辑和测试,我成功了:
#Block empty User-Agents
RewriteCond %{HTTP_USER_AGENT} ^-?$ [NC]
RewriteCond %{HTTP_HOST} !^http://(.*\.)?example\.com [NC]
RewriteCond %{REQUEST_URI} !^/url [NC]
RewriteRule ^ - [F]
也尝试过:
#RewriteCond %{HTTP_HOST} !^(127\.0\.0\.1|localhost) [NC]
#RewriteCond %{REMOTE_ADDR} !^127\.0\.0\.1
没有帮助。只有 REQUEST_URI 有效。
我想屏蔽空的用户代理,但是在网站的脚本中,有一些片段使用了file_get_contents功能,也会被屏蔽。我尝试将 header 添加到 file_get_contents,但效果不佳。所以我尝试使用htaccess绕过本地主机。
这是 htaccess 中的规则:
#Block empty User-Agents
RewriteCond %{HTTP_USER_AGENT} ^-?$
RewriteCond %{HTTP_HOST} !^http://(.*\.)?example\.com [NC]
RewriteRule ^ - [F]
但是不行。我该如何改变?提前致谢。
经过多次编辑和测试,我成功了:
#Block empty User-Agents
RewriteCond %{HTTP_USER_AGENT} ^-?$ [NC]
RewriteCond %{HTTP_HOST} !^http://(.*\.)?example\.com [NC]
RewriteCond %{REQUEST_URI} !^/url [NC]
RewriteRule ^ - [F]
也尝试过:
#RewriteCond %{HTTP_HOST} !^(127\.0\.0\.1|localhost) [NC]
#RewriteCond %{REMOTE_ADDR} !^127\.0\.0\.1
没有帮助。只有 REQUEST_URI 有效。