2013-04-28 133 views
1

我對mod_rewrite有問題。mod_rewrite/foo到/index.php?id=foo AND/foo/foo2到/index.php?id=foo/foo2

我想在標題中重定向這些URI。我用下面的規則

RewriteEngine on 

RewriteCond $1 !^(folders not to be redirected e.g. css|images) 
RewriteCond $1 !^(.*).(file extension e.g. png|css|txt|php) 

RewriteRule ^(.*)$ index.php?id=$1 [L] 

,如果我把所有的資源文件夾中,只工作,否則它會告訴我:

"/foo/index.php" not found. 

所以要解決,我把所有的資源文件夾中「www」

但是,當我嘗試從例如子文件夾加載資源「富」它告訴我:

The requested URL "/foo/foo2" was not found on this server. 

我如何可以加載資源來自像「/富/ foo2的」,甚至是「/富/ foo2的/ foo3」子文件夾?

我該如何解決這個問題,在文件夾中自動搜索index.php?

回答

1

我相信你可以使用以下來達到你想要的效果。它不會通過文件擴展名進行過濾,而是檢查文件是否確實存在。有一點是有點不同的是,它首先附加了一個尾部的斜線到你的鏈接,這可能不是你想要的。

RewriteEngine on 
RewriteBase/

# This appends a trailing slash. You will have to update http://so with your domain. 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_URI} !(.*)/$ 
RewriteRule ^(.*)$ http://so/$1/ [L,R=301] 

# This does the internal redirect 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^(.*)/$ /index.php?id=$1 [L] 

如果你不想拖尾斜線,你可以使用下面的

RewriteEngine on 
RewriteBase/

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^(.*)$ /index.php?id=$1 [L] 
+0

你說得對。我不想在最後追加那些醜陋的尾部斜線。但是如何刪除它們或在沒有它們的情況下重寫? – 2013-04-28 16:10:04

+0

@JuliusRickert,我添加了另一個選項,沒有結尾的斜槓。 – 2013-04-28 16:23:59

+0

我認爲「[NC]」是不需要的,但是「[L]」必須在那裏。我對嗎? – 2013-04-28 20:16:55

相關問題