2015-05-17 13 views
0

在我的單頁面webapp中,我使用了html5歷史API,以便網址可以具有REST模式(/ section1/stuff1 ..),並且我正在計劃根據url路徑,使某種JavaScript路由器導航到頁面的幾個部分。在.htaccess中編寫規則以匹配正確的圖像文件夾位置

現在我還在本地服務器(WAMP)上,我增加了一個.htaccess文件:

RewriteEngine On 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule (.*) index.php/$1 [L] 

到應用程序根目錄包含的頁面的某些章節引用該URL的路徑(例如,子域/章節N)總是可以重定向到的index.php和重定向成功所有外部ressources未能加載和我得到:

Resource interpreted as Image but transferred with MIME type text/html: "http://localhost/subdomain/section1/images/imgname.gif". 

,它的邏輯,因爲圖像文件夾位於應用程序根一個d不在/section1文件夾下,而.htaccess規則RewriteRule (.*) index.php/$1 [L]應僅取/images/imgname.gif部分,並在http://localhost/subdomain/後面進行協調。

我發現this作爲一個類似的問題,所以我重寫.htaccess文件是這樣的:

RewriteEngine On 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^/?section1/(.+)$ index.php/$1 
RewriteRule (.*) index.php/$1 [L] 

,但我得到了500 Internal Server Error

回答

1

這兩個規則:

RewriteRule ^/?section1/(.+)$ index.php/$1 
RewriteRule (.*) index.php/$1 [L] 

將有可能都被應用到一個單一的URL,因爲第一條規則之後沒有[L](最後)的標籤。所以像這樣的URL:

section1/stuff/page1.html 

將由第一條規則被轉換成這樣:

index.php/stuff/page1.html 

,然後將得到送入第二個規則,並轉換爲這樣的:

index.php/index.php/stuff/page1.html 

這很可能是導致500內部服務器錯誤的原因。如果添加[L]第一規則,然後第二個規則將不會在第一條規則相匹配的URL的情況下應用,並適用於:

RewriteRule ^/?section1/(.+)$ index.php/$1 [L] 

如果你不想讓你的圖像的URL重寫,那麼只需刪除第二個RewriteRule(這實際上使得[L]冗餘)。

+0

我將[L]從最後一行移到上一行,但仍然得到了500錯誤,因爲實際上這兩個規則都是重定向所必需的,因爲即使url http:// localhost/subdomain/section1應該由規則處理在最後一行(RewriteRule(。*)index.php/$ 1)中,因爲section1本身只是DOM的一個子元素而不是頁面。 – Bardelman

+0

改變最後兩行的順序並沒有解決它,因爲它看起來像一旦處理了第一個RewriteRule(RewriteRule(。*)index.php/$ 1),它就不會將手伸向下一個規則。 – Bardelman

+0

我犯了一個錯誤;它不是我需要的串行處理,但是RewriteRule^/?section1 /(.+)$ index.php/$ 1規則應該只處理某些文件擴展名(.png,.jpg,..)特定的url。怎麼可能? – Bardelman

相關問題