是否通过Apache将特定的用户代理限制为Internet Explorer? [英] Limiting Specific User Agent to Internet Explorer via Apache?

查看:41
本文介绍了是否通过Apache将特定的用户代理限制为Internet Explorer?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图限制我的网站,使其仅允许具有以下内容的用户代理访问我的网站:

I am trying to limit my site to only allow User Agents with the following to be able to hit my site:

Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko

当前在我的 .htaccess 文件(/var/www/html/)中,我具有以下内容:

Currently in my .htaccess file (/var/www/html/) I have the following:

RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} !Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko [NC]
RewriteRule ^ - [F,L]

推荐答案

通常, CondPattern ( RewriteCond 指令的第二个参数)是一个正则表达式.因此,您需要将其转换为正则表达式-特别是,您需要反斜杠转义文字点和空格.但是,改用词典上的等式运算符(即)会更容易. = 前缀.然后,它匹配文字字符串,而不是正则表达式.

Ordinarily, the CondPattern (2nd argument to the RewriteCond directive) is a regular expression. So, you would need to convert this to a regex - notably, you would need to backslash-escape the literal dots and spaces. However, it would be easier to use the lexicographical equality operator instead, ie. = prefix. This then matches a literal string, not a regex.

用户代理字符串中的空格是一种特殊情况,因为空格是Apache配置文件中的参数分隔符.这些要么需要反斜杠转义(或者,如果使用正则表达式,则使用 \ s 速记字符类),或者将整个参数括在双引号中.

The spaces in the user-agent string are a special case since the space is an argument separator in Apache config files. These either need to be backslash escaped (or use the \s shorthand character class if using a regex), or enclose the whole argument in double quotes.

请尝试以下操作:

RewriteCond %{HTTP_USER_AGENT} "!=Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko"
RewriteRule ^ - [F]

L 标志与 F 标志不是必需的-这是隐含的.如果您只想只允许一个用户代理,那么您就不需要条件上的 NC 标志.

The L flag is not required with the F flag - it is implied. If you specifically only want to allow that one user-agent then you don't need the NC flag on the condition.

这篇关于是否通过Apache将特定的用户代理限制为Internet Explorer?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆