![](/img/trans.png)
[英].htaccess only allow access and redirect to root index.php for only one directory
[英].htaccess only allow access to index.php and a directory
我在用
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 文件是可訪問的。
但是對於任何其他 url,我得到 403,例如 /mysite/home/ 被禁止,即使我希望它正常工作。
我該怎么做才能得到預期的行為?
我的完整 .htaccess 文件是:
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]
您可以使用:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php
那是我用於我的應用程序的 .htaccess。 在public目錄下,我只放了我需要的index.php和JS、CSS和圖片文件,不需要過濾掉。 任何其他文件(例如上傳)都保存到公共文件夾之外的文件夾中 - 因此無法直接訪問它們 - 只能通過讀取它們的 php 腳本。 我相信這是“最佳實踐”。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.