2010-12-09 99 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 
相關問題