2012-07-14 44 views
2

我試圖用CodeIgniter這些天。.htaccess隱藏index.php與相關鏈接問題

默認情況下,它有這樣的

http://www.example.com/index.php/home 

home在URI的index.php文件是控制器

內部的功能,但我不希望index.php所以我創建.htaccess文件:

Allow from all 
RewriteEngine On 
RewriteBase/

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

RewriteCond $1 !^(index\.php|img|robots\.txt|script|css) 
RewriteRule ^(.*)$ index.php/$1 [L] 

基本上它會自動添加「index.php」,除非cond匹配。

但是這裏的東西:
對於URI是這樣的:http://www.example.com/home
一切工作正常。

對於這樣的URI:http://www.example.com/home/
然後,我的網頁中的相關鏈接變得瘋狂。
例如: css/common.css被重定向到index.php/home/css/common.css導致未找到css文件。

日誌是這樣的:

strip per-dir prefix: [dir to my site]/home/css/common.css -> home/css/common.css 
applying pattern '^(.*)$' to uri 'home/css/common.css' 
rewrite 'home/css/common.css' -> 'index.php/home/css/common.css' 
add per-dir prefix: index.php/home/css/common.css -> [dir to my site]/index.php/home/css/common.css 
trying to replace prefix [dir to my site]/ with/
internal redirect with /index.php/home/css/common.css [INTERNAL REDIRECT] 
add path info postfix: [dir to my site]/index.php -> [dir to my site]/index.php/home/css/common.css 
strip per-dir prefix: [dir to my site]/index.php/home/css/common.css -> index.php/home/css/common.css 
applying pattern '^(.*)$' to uri 'index.php/home/css/common.css' 
pass through [dir to my site]/index.php 

我該如何解決這個問題?

+0

+1添加日誌! – Ansari 2012-07-14 00:44:25

回答

2

這裏的問題不是你的重寫規則 - 它們很好。發生了什麼事是瀏覽器認爲/home/最後一個斜槓是一個真實的目錄,因此它將所有相關鏈接前綴到它認爲它所在的目錄中。瀏覽器然後破壞對CSS文件的請求,路徑爲/home/css/common.css,它自然會被重寫,因爲它不符合您的支票。

我在這裏看到的最快捷的解決方案是爲CSS文件使用絕對路徑。只需將它們鏈接爲/css/common.css,而不是現在的相關鏈接。

+0

其實我在網站中使用了很多類似的鏈接。只要所有鏈接都是從我的網頁生成的,它們都可以。那麼我可以忽略這個問題嗎? – NSF 2012-07-14 01:16:41

+0

我不知道你是否可以忽略它 - 如果它找不到CSS文件,是不是受影響? – Ansari 2012-07-14 01:36:58

+0

是的,但只要鏈接是由我的網頁生成的,他們將不會包含最後一個斜槓,看起來工作正常 – NSF 2012-07-14 07:10:37