全部拒绝,只允许通过htaccess一个IP

我试图拒绝所有,只允许一个IP。 但是,我想有以下的htaccess为单个IP工作。 我没有find一种方法来同时工作:拒绝所有,只允许一个,加上以下选项:

<IfModule mod_rewrite.c> RewriteEngine On RewriteBase / #Removes access to the system folder by users. #Additionally this will allow you to create a System.php controller, #previously this would not have been possible. #'system' can be replaced if you have renamed your system folder. RewriteCond %{REQUEST_URI} ^system.* RewriteRule ^(.*)$ /index.php?/$1 [L] #When your application folder isn't in the system folder #This snippet prevents user access to the application folder #Submitted by: Fabdrol #Rename 'application' to your applications folder name. RewriteCond %{REQUEST_URI} ^application.* RewriteRule ^(.*)$ /index.php?/$1 [L] #Checks to see if the user is attempting to access a valid file, #such as an image or css document, if this isn't true it sends the #request to index.php RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php?/$1 [L] </IfModule> <IfModule !mod_rewrite.c> # If we don't have mod_rewrite installed, all 404's # can be sent to index.php, and everything works as normal. # Submitted by: ElliotHaughin ErrorDocument 404 /index.php </IfModule> 

有没有办法做到这一点?

 order deny,allow deny from all allow from <your ip> 

这可以通过使用为该任务devise的指令来改进。

 ErrorDocument 403 /specific_page.html Order Allow,Deny Allow from 111.222.333.444 

其中111.222.333.444是您的静态IP地址。

当使用“Order Allow,Deny”指令时,请求必须匹配“允许”或“拒绝”,否则请求将被拒绝。

http://httpd.apache.org/docs/2.2/mod/mod_authz_host.html#order

我知道这个问题已经有了一个可以接受的答案,但是Apache的文档说:

由mod_access_compat提供的Allow,Deny和Order指令已被弃用,并将在未来的版本中消失。 你应该避免使用它们,并避免过时的教程推荐使用它们。

所以,更有前途的答案是:

 <RequireAll> Require ip xx.xx.xx.xx yy.yy.yy.yy </RequireAll> 

希望我已经帮助防止这个页面成为那些“过时的教程”之一。 🙂

上述的稍微修改版本,包括一个自定义页面显示给那些被拒绝访问的人:

 ErrorDocument 403 /specific_page.html order deny,allow deny from all allow from 111.222.333.444 

…那么那些来自111.222.333.444的请求将会看到specific_page.html

(发表评论看起来很糟糕,因为新的线路丢失)

改善一点以前的答案:

 ErrorDocument 403 /maintenance.html Order Allow,Deny Allow from #.#.#.# 

哪里:

  • #.#.#.#是您的IP: 什么是我的IP地址?
  • 对于maintenance.html这里有一个很好的例子: 简单的维护页面

您可以在htaccess中使用以下内容来允许和拒绝访问您的网站:

 SetEnvIf remote_addr ^1\.2\3\.4\.5$ allowedip=1 Order deny,allow deny from all allow from env=allowedip 

我们首先设置envvariablesallowedip,如果客户端的IP地址匹配模式,如果模式匹配,则envvariablesallowedip被赋值为1

在下一步中,我们使用允许,拒绝指令来允许和拒绝访问该网站。 Order deny,allow表示denyallow的顺序。 deny from all这一行告诉服务器否认每个人。 allow from env=allowedip的最后一行allow from env=allowedip访问我们设置envvariables的单个IP地址。

用您允许的IP地址replace1\.2\.3\.4\.5

参考:

我不能使用403方法,因为我想维护页面和页面图像在我的服务器上的一个子文件夹,所以使用下面的方法redirect到每个人的“维护页面”,而不是一个IP *

 RewriteEngine on RewriteCond %{REMOTE_ADDR} !**.**.**.* RewriteRule !^maintenance/ http://www.website.co.uk/maintenance/ [R=302,L] 

来源: 创build一个隐藏你的WordPress博客的页面

如果你想使用mod_rewrite进行访问控制,你可以使用像用户代理,HTTP引用,远程地址等条件。

 RewriteCond %{REMOTE_ADDR} !=*.*.*.* #you ip address RewriteRule ^$ - [F] 

Refrences:

你可以有多个IP甚至其他一些允许用户,主机名,…更多信息在这里https://www.askapache.com/htaccess/setenvif/

 SetEnvIf remote_addr ^123.123.123.1$ allowedip=1 SetEnvIf remote_addr ^123.123.123.2$ allowedip=1 SetEnvIf remote_addr ^123.123.123.3$ allowedip=1 SetEnvIf remote_addr ^123.123.123.4$ allowedip=1 Order deny,allow deny from all allow from env=allowedip