2011-12-30 62 views
2

現在我在我的網站的根目錄下有幾個文件夾(img,css,js和ico)。但是,我想將它們移動到名爲public_html的新目錄中。例如,而不是圖像文件夾位於/ img中,而是位於/ public_html/img中。但是我遇到了問題,我懷疑這是我的.htaccess文件的問題。mod_rewrite無法正常工作 - 訪問靜態資源

Options -Indexes 
Options +FollowSymLinks 
RewriteEngine On 

ErrorDocument 404 static/404.html 
ErrorDocument 403 static/403.html 


RewriteCond %{REQUEST_FILENAME} !-f 

# ---------- Custom Routes ---------------- 



# ----------------------------------------- 

RewriteRule ^(js|css|img|ico)\/(.+)$ public_html/$1/$2 
RewriteRule ^(.*)$ index.php?r=$1 [L,QSA] 

我可以訪問/public_html/css/style.css就好了,但是當我在第一次重寫規則添加一行並嘗試訪問/css/style.css,這是行不通的。

任何人都可以找出原因嗎?謝謝。

回答

1

[L]標誌添加到第一條規則。否則它會直接跳到第二條規則並嘗試將它傳遞到r=index.php。通過因RewriteCond %{REQUEST_FILENAME} !-f

RewriteRule ^(js|css|img|ico)\/(.+)$ public_html/$1/$2 [L] 

# Update: Try using the RewriteCond before this line 
# as well as where you have it earlier 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^(.*)$ index.php?r=$1 [L,QSA] 
+0

這似乎是[L]標誌不能正常工作,因爲當我註釋掉第二行它完美public_html工作訪問它。 – Ryan 2011-12-30 22:29:52

+0

@RLC嘗試在最後一行之前再次添加'RewriteCond',就像我剛纔編輯的那樣。 – 2011-12-30 22:34:09

+0

謝謝!這解決了它。 – Ryan 2011-12-30 22:38:56