IP Blocking URLs on Apache

久未见 提交于 2020-01-05 04:57:06

问题


I need to block access to my entire site via IP Address except the url /api which should be open to all.

I am currently using ...

<LocationMatch /admin>
    Order Deny,Allow
    Deny from all
    Allow from [MY IP]
</LocationMatch>

this blocks access urls starting with /admin. But I want to block all urls except the ones that start /api.

Chris


回答1:


RewriteEngine On # (only needs to happen once in .htaccess files.

RewriteBase /
RewriteCond %{REMOTE_ADDR} !^10\.103\.18\.104     # <--YOUR IP HERE
RewriteCond %{REQUEST_URI} !^/api    # page or directory to ignore                   
RewriteRule ^(.*)$ http://example.com/no_access.html [R=401] # where to send blocked requests



回答2:


Well you can first block the whole site, then simply allow /api.

<LocationMatch />
    Order Deny,Allow
    Deny from all
    Allow from [MY IP]
</LocationMatch>

<LocationMatch /api>
    Order Deny,Allow
    Allow from all
</LocationMatch>

Sorry I couldn't test it due to the way XAMPP is configured on my PC. Pray it works.



来源:https://stackoverflow.com/questions/1554648/ip-blocking-urls-on-apache

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!