我正在使用
RewriteEngine On
RewriteRule ^.*$ ./index.php
通过 index.php 重定向我所有的网站流量,让我的 php 代码获取 request_uri 并相应地路由每个请求。
我想限制对除包含 js 文件、css 文件、图像等的文件/目录之外的所有其他文件/目录的访问。
我发现我可以使用:
Order deny,allow
Deny from all
<Files index.php>
Order Allow,Deny
Allow from all
</Files>
然后在公共(public)目录中有另一个 .htaccess 文件以允许某些文件类型。
当我导航到 localhost/mysite/index.php 时,它工作正常,因为 index.php 文件是可访问的。
但对于任何其他网址,我都会收到 403,例如/mysite/home/被禁止,即使我希望它能正常工作。
我怎样做才能获得预期的行为?
我的完整 .htacces 文件是:
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^.*$ ./index.php
Order deny,allow
Deny from all
<Files index.php>
Order Allow,Deny
Allow from all
</Files>
编辑:/public/.htaccess 目前看起来像这样:
<Files ~ "\.(xml|css|jpe?g|png|gif|js|pdf)$">
Allow from all
</Files>
最佳答案
在 /mysite/.htaccess
中尝试这段代码:
DirectoryIndex index.php
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^ index.php [L]
RewriteRule !^(public/|index\.php) [NC,F]
关于php - .htaccess 只允许访问 index.php 和一个目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30446569/