2013-03-23 24 views
1

我有一個網站運行在localhost/pm和RewriteBase正確設置爲/pm/。我的文檔中有一個鏈接標籤:<link ... href="themes/default/css/default.css">基本的URL重寫:如何有一個CSS鏈接,IMG SRC等,忽略它?

當url爲localhost/pmlocalhost/pm/foo時,CSS正常工作。但是,如果URL中有更多斜線,則樣式表更改爲foo/themes/default/css/default.css時,將顯示相對URL localhost/pm/foo/bar

如何在不必在鏈接標記中添加某種PHP路徑分辨率的情況下使其工作?

# invoke rewrite engine 
RewriteEngine On 
RewriteBase /pm/ 

# Protect application and system files from being viewed 
RewriteRule ^(?:system)\b.* index.php/$0 [L] 

# Allow any files or directories that exist to be displayed directly 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 

# Rewrite all other URLs to index.php/URL 
RewriteRule .* index.php/$0 [PT] 

編輯:

基本上我現在需要的是這樣的: 如果請求中包含的文件夾名稱/themes/廢料一切,這是/themes/之前,其餘改寫爲/pm/themes/...

我想它是這樣的: RewriteRule ^.*(/themes/.*)$ /pm/themes/$1但我得到一個內部服務器錯誤。爲什麼?

如果我不喜歡這樣寫道:RewriteRule ^.*(/themes/.*)$ /pm/themes/(即只是刪除從最終$ 1)並使用URL http://localhost/pm/foo/themes/foo/所產生的物理位置是http://localhost/pm/themes這是什麼太期待,這反過來又意味着,至少在我的正則表達式是正確。我錯過了什麼?

+0

您必須排除使用RewriteCond重寫css,請參閱此答案http://stackoverflow.com/a/15348786/1741542或重寫CSS請求,請參閱http://stackoverflow.com/a/15535329/1741542 – 2013-03-23 17:20:51

+0

沒有幫助。然而,使用標籤。 – 2013-03-23 17:37:59

+0

使用'base'標籤時請注意,有關詳細信息,請參閱http://stackoverflow.com/q/1889076/1741542。如果你的css文件位於'/ pm/themes/default/css /'下面,你也可以重寫這些文件。 – 2013-03-23 17:53:58

回答

1

的重寫規則幾乎是正確

RewriteRule ^.*(/themes/.*)$ /pm/themes/$1 

這改寫http://localhost/pm/foo/themes/default/css/default.csshttp://localhost/pm/themes/themes/default/css/default.css,這是一個themes太多。使用這個代替

RewriteRule /themes/(.*)$ /pm/themes/$1 [L] 

但現在你有無限重寫循環,因爲/pm/themes/..一次又一次改寫。爲了防止這種情況,你需要一個RewriteCond排除/pm/themes

RewriteCond %{REQUEST_URI} !^/pm/themes/ 
RewriteRule /themes/(.*)$ /pm/themes/$1 [L] 

現在請求被重寫只有一次,你就大功告成了。

0

你可能需要您的RewriteRule之前添加下面幾行:

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 

如果請求的文件或目錄不存在它只會評估你的重寫規則。

你應該張貼您的.htaccess文件,以便我們可以提供更好的建議

+0

已經有了。 – 2013-03-23 17:12:03

相關問題