2010-12-09 59 views
119

我想拒绝所有,只允许一个IP。但是,我想要有以下htaccess为单个IP工作。我没有找到一个办法让两个工作:全部拒绝,只允许一个,再加上以下选项:拒绝所有,只允许通过htaccess一个IP

<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> 

有没有一种方法,使这项工作?

回答

277
order deny,allow 
deny from all 
allow from <your ip> 
+0

这对我工作,谢谢! – 2012-10-08 23:51:15

+69

注意! Apache对htaccess中的空间很敏感。不允许拒绝,允许之间的任何空间。即不写命令拒绝,允许。 – Rabiees 2013-06-11 21:53:32

+2

信息: - 它也适用于IPv6!只需添加另一行'allow from your ipv6' – 2013-10-31 13:54:09

28

以上的稍作修改的版本,包括自定义页面将显示给那些谁得到拒绝访问:

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

......和这样这些请求不从111.222.333.444来将看到specific_page.html

(张贴这作为注释看起来可怕,因为新线丢失)

38

这可以通过使用专门为塔的指令来改善t任务。

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

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

当使用“Order Allow,Deny”指令时,请求必须匹配Allow或Deny,如果两者都不符合,请求将被拒绝。

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

2

您可以使用下面的htaccess的允许和拒绝访问您的网站:

SetEnvIf remote_addr ^1\.2\3\.4\.5$ allowedip=1 

Order deny,allow 
deny from all 
allow from env=allowedip 

我们先设定一个环境变量allowedip如果客户端IP地址匹配的模式,如果模式匹配,则env变量allowedip被赋值为。

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

用您允许的IP地址替换1\.2\.3\.4\.5

Refrences:

50

我知道这个问题已经有一个接受的答案,但Apache documentation说:

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

所以,更面向未来的答案是:

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

希望我帮助防止此页成为那些“过时的教程”中的一个。 :)

1

我无法使用403方法,因为我想将维护页面和页面图像放在我的服务器上的一个子文件夹中,所以使用了以下内容方法重定向到一个“维护页面”适合所有人,但单一的IP *

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

来源:Creating a holding page to hide your WordPress blog

0

你可以有一个以上的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 
相关问题