.htaccess 拒绝来自外部请求的访问

nro*_*fis 5 php .htaccess

我想限制对我网站中某些页面的访问。我在 PHP 中有一些 BL 页面,我想将其访问权限限制为仅内部访问。

我的意思是,如果用户在浏览器中输入这些页面,我希望这些页面将被拒绝,但如果另一个 PHP 页面将调用它们(使用 POST 或 GET 请求),则可以访问它们。

是否可以在 .htaccess 文件中做到这一点?如果是,如何?

Jon*_*Lin 2

需要澄清的是,php 页面不是发送 POST 或 GET 请求的页面,而是浏览器页面,这意味着您无法通过 IP 进行阻止。所以你需要在这里检查引用者。问题是引用者很容易被伪造,因此这并不能保证您会拒绝访问。

您可以使用 mod_rewrite 中的变量检查引用者%{HTTP_REFERER},然后使用该F标志拒绝访问:

RewriteEngine On
# if the request's referer isn't from a php page on your site
RewriteCond %{HTTP_REFERER} !^https?://your-domain.com/.*\.php
# deny access to the list of php files
RewriteRule ^(path/to/protected.php|path/another_protected.php|images/protected.png)$ - [L,F]
Run Code Online (Sandbox Code Playgroud)