douliang2935 2013-09-05 02:00
浏览 51
已采纳

带有重写的特定文件.htaccess的白名单IP

Here's my current situation:

I'm rewriting /api to api.php.

RewriteRule ^apidir/url/new?$ api.php [QSA,NC,L]

Now, I am whitelisting IP's for it. However, when I try and use the Files directive to block access, I can't because it is being rewritten.

<Files "\api.php">
order allow,deny
allow from all
deny from 24.11.95.151
</Files>

so, I tried just using it to block the rewritten directory

<Files "\apimashape2013/url/new">
order allow,deny
allow from all
deny from 24.11.95.151
</Files>

None of these have worked out.

  • 写回答

2条回答 默认 最新

  • doubi1928 2013-09-05 02:33
    关注

    If you have access to the virtualhost you can use this:

    RewriteEngine on
    RewriteMap hosts-deny txt:/home/youraccount/deny_list.txt
    RewriteCond ${hosts-deny:%{REMOTE_ADDR}|NOT-FOUND} !=NOT-FOUND [OR]
    RewriteCond ${hosts-deny:%{REMOTE_HOST}|NOT-FOUND} !=NOT-FOUND
    RewriteCond %{REQUEST_URI} ^/api [NC]
    RewriteRule ^ - [F]
    

    And have the deny_list.txt with the list of IP's or HOST you wish to blacklist:

    193.102.180.41 -
    bsdti1.sdm.de -
    192.76.162.40 -
    

    Read more here.


    Alternatively if you don't have access to the virtualhost, you can change the name of your php file to something unique for example thisismyapi.php and use it like this:

    Options +FollowSymLinks -MultiViews
    
    RewriteEngine On
    RewriteBase /
    
    <Files "thisismyapi.php">
    order allow,deny
    allow from all
    deny from 24.11.95.151
    </Files>
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_URI} !thisismyapi.php
    RewriteRule ^api/?(.*)$ /api/thisismyapi.php [L]
    

    Why do I change it to other name?

    Because this way it will match it anywhere it is so it should work just fine for your needs and will not conflict with other files.

    For example in this way it the file was named index.php it would conflict with index.php in other folders.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器