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 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)