某个文件的白名单IP.htaccess with rewrite



这是我目前的情况:

我正在将/api重写为api.php。

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

现在,我正在为它列出IP白名单。然而,当我尝试使用Files指令来阻止访问时,我做不到,因为它正在被重写。

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

所以,我试着用它来阻止重写的目录

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

这些都没有成功。

如果您可以访问virtualhost,则可以使用以下内容:

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]

并在deny_list.txt中列出您希望列入黑名单的IP或主机:

193.102.180.41 -
bsdti1.sdm.de -
192.76.162.40 -

点击此处阅读更多信息。


或者,如果您无法访问virtualhost您可以将php文件的名称更改为唯一的名称,例如thisismyapi.php,并像这样使用:

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]

我为什么要把它改成其他名字?

因为这样它可以在任何地方匹配,所以它应该可以很好地满足您的需求,不会与其他文件冲突。

例如,如果文件名为index.php,则会与其他文件夹中的index.php发生冲突。

不确定第一个<Files>容器不工作的原因。应该没有任何作用。但是容器需要位于api.php文件所在的目录或父目录中

或者,您可以将其包含在您的htaccess中:

RewriteCond %{REMOTE_ADDR} ^24.11.95.151$
RewriteRule ^/?api.php$ - [L,F]

最新更新