[英]Rewriting a request to a parent directory with mod_rewrite
使用WAMP進行一些本地PHP開發。 將所有請求路由到index.php, 但對樣式表和圖像之類的資產的請求除外,這些請求位於文檔根目錄之外的父目錄中。 目錄布局如下:
C:\WAMP\www\
↳ mySite
↳ public
↳ .htaccess
↳ index.php
↳ assets
↳ style
↳ desktop.css
的.htaccess
RewriteEngine on
RewriteBase /mySite/
RewriteCond %{REQUEST_URI} \.css$ [NC]
RewriteRule ^(.*)$ assets/$1 [L]
RewriteCond %{REQUEST_URI} !-f
RewriteCond %{REQUEST_URI} !-d
RewriteRule ^(.+)\/?$ index.php?url=$1 [L,QSA]
mySite.conf
Alias /mySite/ "C:/WAMP/www/mySite/public/"
<Directory "C:/WAMP/www/mySite/public/">
Options Indexes FollowSymLinks MultiViews
AllowOverride all
Order allow,deny
Allow from all
</Directory>
結果
localhost/mySite/
-按預期返回index.php
localhost/mySite/style/desktop.css
返回內部服務器錯誤
我錯過了什么?
謝謝
這可能是由於循環。
嘗試使用以下代碼來解決此問題:
RewriteEngine on
RewriteBase /mySite/
RewriteRule ^((?!assets/).+?\.css)$ assets/$1 [L,NC]
RewriteCond %{REQUEST_URI} !-f
RewriteCond %{REQUEST_URI} !-d
RewriteRule ^(.+)\/?$ index.php?url=$1 [L,QSA]
正如下面的Anubhava在另一個問題中回答的那樣:
只是為了澄清任何網站都無法訪問其DOCUMENT_ROOT文件夾級別以上的文件。
因此,我應該將文檔根目錄設置為mySite/
,並將所有請求都路由到public/
以實現與公開文檔根目錄相同的安全性。
既然assets/
位於文檔根目錄下,我就可以在OP中根據需要將資產請求重寫到此目錄中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.