.htaccess-拒绝访问文件夹,但允许通过index.php访问文件


.htaccess - Deny access to folder but allow access to file via index.php

我在子文件夹中遇到.htaccess和PHP文件问题。我试图实现的是防止任何人访问我的子文件夹中的任何文件,但我希望我的index.php能够访问这些文件。

DOCROOT/subfolder -> www.somewebsite.com/subfolder/somefile.php
-> Access Denied

但是

[index.php]
<form action="/subfolder/somefile.php">
...
</form>
-> Success!

我很想通过使用.htaccess来解决这个问题。我尝试了deny from all和一些RewriteRule,但这些规则也会扼杀来自index.php的任何请求。我试过

Order deny,allow
Deny from all
Allow from 127.0.0.1
Allow from somewebsite.com
Satisfy Any

但是来自index.php的请求被拒绝了。有人能帮忙吗?

这是人们的误解。仅仅因为从另一个PHP文件链接到PHP文件,并不意味着index.php文件正在访问它们。最终用户/浏览器仍在访问它们,只是index.php文件告诉它该去哪里。与访问方式完全无关。在这两种情况下,浏览器都在访问它们。

你能做的最好的事情就是看看裁判场。它可以很容易地伪造来绕过这一点,但这是你唯一能做的事情。

RewriteEngine On
RewriteCond %{HTTP_REFERER} !^https?://(example.com|127'.0'.0'.1) [NC]
RewriteRule ^subfolder/ - [L,F]

其中"example.com"是您的网站。

RewriteEngine On
RewriteCond %{HTTP_REFERER} !^http://www.hello.com/index.php
RewriteRule .*subfolder/somefile'.php - [NC,F]

第二行检查访问者是否不是来自某个url。第三行阻止他们访问somefile.php

在.htaccess中,您可以将任何请求重定向到该目录中除index.php以外的文件,如下所示:

<directory "DOCROOT/subfolder">
    RewriteCond %{REQUEST_FILENAME} !=/DOCROOT/subfolder/index.php
    RewriteRule ^/(.+)$ redirect.php [L]
</directory>