2013-05-20 34 views
0

在使用.htaccess文件拒绝/允许IP阻止之前是否可以应用重定向?是否可以先应用重写,然后使用.htaccess拒绝/应用IP块?

我试过了,但没有重定向未加入白名单的用户被阻止,所以这意味着拒绝/允许部分被执行,即使他们应该被重定向。重定向部分工作正常,因为我测试它没有任何IP阻塞。我预计重写[L]标志会在到达IP阻塞部分之前“停止”.htaccess执行。

RewriteCond %{HTTP_HOST} !^blog\.mysite\.com$ [NC] 
RewriteRule (.*) http://www.mysite.com [L,NC,R=301] 

Order deny,allow 
Deny from all 
Allow from xxx.xxx.xxx.xxx 

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ /index.php?/$1 [L] 

为什么我这样做是因为我要保护访问http://www.mysite.com/blog同时还显示相同的404是由网站的其它无效页面上显示。如果先做IP阻止,我不能显示由站点框架生成的相同的404页面。

我做错了什么或者它只是不可能这样做?

回答

2

你可以使用一个重写条件检查IP地址:

RewriteEngine On 
RewriteBase/

#always redirect blog.mysite.com to www.mysite.com 
RewriteCond %{HTTP_HOST} ^blog\.mysite\.com$ [NC] 
RewriteRule (.*) http://www.mysite.com [L,NC,R=302] 

#don't redirect if the accessDenied.php page is accessed 
RewriteCond %{REQUEST_FILENAME} ^accessDenied\.php$ 
RewriteRule (.*) - [L] 

#redirect all not whitelisted IPs 
RewriteCond %{REMOTE_ADDR} !^111\.111\.111\.111 [or] 
RewriteCond %{REMOTE_ADDR} !^222\.222\.222\.222 
RewriteRule (.*) accessDenied.php [R=302,L] 

#only whitelisted IPs will use this rewrite rule 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule (.*) index.php?/$1 [L,R=302] 

如果IP匹配,第二重写规则将不被满足,那么什么都不会发生。但如果IP不匹配,用户将被重定向到404错误页面。

+0

谢谢,但这不是我想要实现的。我可能很难解释这个问题。我想只允许访问某些网站的IP地址,因此必须阻止其他IP。在IP阻塞之前,应该有ReWrite条件来查看是否试图访问特定的URL,并且在这种情况下使用重写而不是进行IP阻塞。如果我正确理解您的解决方案,则不会阻止IP阻止除IP之外的其他IP。 – Laowai

+0

我更新了我的答案。现在blog.mysite.com应该总是被重定向到www.mysite.com。如果访问其他网址,只有列入白名单的IP才能看到该页面,所有其他IP将被重定向到错误页面。但我目前无法测试代码,所以我不能100%确定这是正常工作 – Christopher

+0

我会尽快测试并让您知道结果,谢谢。 – Laowai

相关问题