簡體   English   中英

使用htaccess阻止訪問除我的IP地址以外的一系列URL

[英]Using htaccess block access to a range of URLs except for my IP address

我如何才能為除我的IP地址之外的所有地址屏蔽范圍或URL。

所以...

允許所有用戶使用http://mydomain.com/ *

除了我的IP以外,將http://mydomain.com/thisdirectoryandallcontents/ *禁止所有

我從另一個站點找到了一些可能有效的代碼,但是我沒有htaccess的技能來適應它

Options +FollowSymlinks
RewriteEngine on

#urls to exclude
RewriteCond %{REQUEST_URI} !/url-to-exclude-1$
RewriteCond %{REQUEST_URI} !/url-to-exclude-2$
RewriteCond %{REQUEST_URI} !^/images$
RewriteCond %{REQUEST_URI} !^/css$
RewriteCond %{REQUEST_URI} !^/$

#ip to allow access
RewriteCond %{REMOTE_HOST} !^11\.11\.11\.11$

#send to root
RewriteRule .? /http://www.mydomain.com [R=302,L]

我有類似的要求,並且在將我的頁面(或部分)進行維護時,基本上也要執行相同的操作。 htaccess文件的內容如下所示:

Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_URI} /thisdirectoryandallcontents
RewriteCond %{REMOTE_ADDR} !=111.111.111.111
RewriteRule ^.*$ /maintenance.php [R=302,L]

我會根據您的要求逐步說明:

  1. 每個用戶都應該能夠訪問主頁http://mydomain.com/-很酷,不需要任何配置。
  2. 第一個RewriteCond檢查請求是否位於/ thisdirectoryandallcontents文件夾中。
  3. 第二個RewriteCond檢查IP地址是否為111.111.111.111以外的其他地址(在此必須編寫IP地址)。
  4. 如果兩個條件都適用,我們會將用戶重定向到/maintenance.php。 在這里,我通常向用戶輸出一條消息,該頁面當前正在維護中。 您可能會顯示一條消息,表明該用戶無權訪問/ thisdirectoryandallcontents。 如果您願意,也可以將用戶重定向到主頁,而不是將用戶重定向到維護頁面。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM