简体   繁体   English

如何限制特定的 IP 范围从查看 .htaccess 中的特定页面?

[英]How do I restrict specific IP ranges from viewing a specific page in .htaccess?

I'm trying to only allow specific IP ranges to access /user/login.我试图只允许特定的 IP 范围访问 /user/login。 Denying anything under the /user sub-directory would suffice.拒绝 /user 子目录下的任何内容就足够了。

So far I've tried:到目前为止,我已经尝试过:

<Location /user>
    Order deny,allow  
    Deny from all
    Allow from 123.45.116.0/24
    Allow from 123.225.232.0/23
    Allow from 123.225.114.0/23
    Allow from 123.9.53
</Location>

and

RewriteEngine on
RewriteCond %{REMOTE_ADDR} !^123\.45\.116\.0/24$
RewriteCond %{REMOTE_ADDR} !^123\.225\.232\.0/23$
RewriteCond %{REMOTE_ADDR} !^123\.225\.114\.0/23$
RewriteCond %{REMOTE_ADDR} !^123\.9\.534$
RewriteRule ^user/? - [F,NC]

and

 RewriteCond %{HTTP:X-FORWARDED-FOR} !^123\.45\.116\.0/24
 RewriteCond %{HTTP:X-FORWARDED-FOR} !^123\.225\.232\.0/23
 RewriteCond %{HTTP:X-FORWARDED-FOR} !^123\.225\.114\.0/23
 RewriteCond %{HTTP:X-FORWARDED-FOR} !^123\.9\.53\.
 RewriteRule ^(user|user/login) - [F]

no luck.没运气。 Could anyone out there please help?有人可以帮忙吗? I always struggle with htaccess.我总是与 htaccess 斗争。

In case you are looking to allow specific IP ranges(taken from your efforts) and URI should start from user then you may try following.如果您希望允许特定的 IP 范围(取自您的努力)并且 URI 应该从user开始,那么您可以尝试关注。 So in case IP addresses doesn't come in mentioned IP ranges and its trying to hit URL like http://lcoalhost:80/user (as an example) then it will Forbid that page. So in case IP addresses doesn't come in mentioned IP ranges and its trying to hit URL like http://lcoalhost:80/user (as an example) then it will Forbid that page. Here is Online demo for regex used这是使用的正则表达式的在线演示

RewriteEngine ON
RewriteCond %{REMOTE_ADDR} !^123\.45\.116\.([0-9]|[0-1][0-9]|2[0-4])$
RewriteCond %{REMOTE_ADDR} !^123\.225\.232\.([0-9]|[0-1][0-9]|2[0-3])$
RewriteCond %{REMOTE_ADDR} !^123\.225\.114\.([0-9]|[0-1][0-9]|2[0-3])$
RewriteCond %{REMOTE_ADDR} !^123\.9\.53
RewriteRule ^user/? - [F,NC,L]

Please make sure you clear your browser cache before testing your URLs.请确保在测试 URL 之前清除浏览器缓存。



OR you could use a single condition also, use either above OR following only at a time.或者你也可以使用一个条件,一次只使用上面的或后面的。

RewriteEngine ON
RewriteCond %{REMOTE_ADDR} !(^123\.45\.116\.([0-9]|[0-1][0-9]|2[0-4])$)|(^123\.225\.232\.([0-9]|[0-1][0-9]|2[0-3])$)|(^123\.225\.114\.([0-9]|[0-1][0-9]|2[0-3])$)|(^123\.9\.53)
RewriteRule ^user/? - [F,NC,L]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM