apache 使用 htaccess 限制 IP
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/593922/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
IP restriction with htaccess
提问by ARemesal
I want to restrict an entire site in such a way that just two IPs could enter the site. I have the following in my .htaccess (at the root of site):
我想以只有两个 IP 可以进入该站点的方式限制整个站点。我的 .htaccess(在站点的根目录)中有以下内容:
ErrorDocument 403 http://www.example.com/views/error403.html
Order Deny,Allow
Deny from all
Allow from 311.311.311 322.322.322.322
ErrorDocument 404 /views/error404.html
ErrorDocument 500 views/error500.html
(Obviously, these are fake IPs, in my .htaccess they are the right IPs)
(显然,这些是假 IP,在我的 .htaccess 中它们是正确的 IP)
As you can see, I allow just 322.322.322.322 and all IPs from 311.311.311.0/24, and deny for the rest of people. What I want is that when anybody enter the site from another IP, he'll view the error403.html page.
如您所见,我只允许 322.322.322.322 和来自 311.311.311.0/24 的所有 IP,并拒绝其他人。我想要的是,当有人从另一个 IP 进入站点时,他会看到 error403.html 页面。
The filter is working fine, but not the redirection. When I try to enter the site from a deny IP, I see an Apache message:
过滤器工作正常,但不能重定向。当我尝试从拒绝 IP 进入站点时,我看到一条 Apache 消息:
Found
The document has moved here
Where "here" is a link to error403.html.
其中“here”是指向 error403.html 的链接。
I think I'm restricting even the error403.html page.
我想我甚至限制了 error403.html 页面。
How can I do this restriction, but allowing the view of the error page? Should I move error403.html page to another directory (i.e., /views/error/ ) and put other .htaccess in it, allowing in that file all the IPs?
我怎样才能做到这个限制,但允许查看错误页面?我是否应该将 error403.html 页面移动到另一个目录(即 /views/error/ )并将其他 .htaccess 放入其中,允许该文件中的所有 IP?
Thank you in advance!
先感谢您!
采纳答案by vladr
Yes, you have answered your own question. :) Move all non-protected pages into another directory with its own .htaccesscontaining the proper Allowand Deny.
是的,你已经回答了你自己的问题。:) 将所有不受保护的页面移动到另一个.htaccess包含正确Allow和Deny.

