如何:使用htaccess限制url到特定的IP地址

我需要阻止一个特定的url,看起来像www.domain.com/admin/login/? 使用htaccess和授予权限访问只有特定IP地址的url。

我们已经尝试了这个,部分工作。 “?” 在URL似乎是给的问题。 当我包括? 在htaccess中,如下所示的URL,它不会按照需要工作的方式工作。 我在这里做错了什么?

Options +FollowSymlinks RewriteEngine on RewriteCond %{REMOTE_ADDR} !=xxx\.xxx\.xxx\.xxx RewriteRule admin/login/?$ / [R=301,L] 

现有的htaccess

更新:我有一个自定义的CMS,并没有一个典型的树结构,即http://www.mysite.com/folder不对应于/var/www/folder ,实际上http://www.mysite.com/folder根本没有单个文件或目录表示。 因此/folder没有对应的.htaccess文件。 我看不到如何将URL而不是目录附加到重写规则。 这里是我一直在玩的重写概念:

这会工作吗? 我在网上find了这个:

 <Location /folder> Order deny,allow deny from all allow from xxx.xxx.xxx.xxx allow from xxx.xxx.xxx.xxx </Location> 

更新#2:问题已经解决。 问题在于我使用Cloudflare的原因。 这使得从cloudflare到URL的所有请求。 在服务器上安装mod_cloudflare并使用下面的脚本,我成功地阻止了只允许指定IP的公开访问。

 <IfModule mod_rewrite.c> RewriteEngine on RewriteBase / RewriteCond %{REQUEST_URI} ^(.*)admin(.*)$ RewriteCond %{REMOTE_ADDR} !^xxx\.xxx\.xxx\.xxx$ RewriteRule .* / [R=302,L] </IfModule> 

此规则应该可以阻止该网址:

 RewriteEngine on RewriteCond %{REMOTE_ADDR} !^xxx\.xxx\.xxx\.xxx$ RewriteRule ^admin/login/? - [F,NC] 

更新:将此代码放在/admin/.htaccess

 RewriteEngine on RewriteCond %{REMOTE_ADDR} !^xxx\.xxx\.xxx\.xxx$ RewriteRule ^login/? - [F,NC] 
 # only allow acces to these urls from white listed IPs Options +FollowSymlinks RewriteEngine on #the urls that should be checked RewriteCond %{REQUEST_URI} ^(/login|/wp-login.php|/wp-admin).*$ RewriteCond %{REMOTE_ADDR} !=xxx.xxx.xxx.xxx # or this ip RewriteCond %{REMOTE_ADDR} !=xxx.xxx.xxx.xxx # if not fail RewriteRule ^.*$ / [F] 

? 是一个特殊的正则表达式字符,所以你必须用一个反斜杠: \?

在.htaccess文件中试试这个:

 RewriteEngine On RewriteCond %{REQUEST_URI} ^/admin RewriteCond %{REMOTE_ADDR} !=10.0.0.1 RewriteCond %{REMOTE_ADDR} !=10.0.0.2 RewriteCond %{REMOTE_ADDR} !=10.0.0.3 RewriteRule ^(.*)$ - [R=403,L] 

如果网址以/ admin开头,并且远程地址不是列出的三个地址之一,请快速发送浏览器。

参考: https : //www.concrete5.org/community/forums/chat/restrict-urls-starting-with-abc-to-specific-ips-.htaccess-guru