2012-02-01 49 views
3

我在位於目錄中的.htaccess文件這個規則命名爲clips/mod_rewrite的使用相對路徑重定向

RewriteRule ^mlk/?$ segment/index.php?clip=1 [R=301,QSA,L] 

什麼我打算是,當有人訪問http://example.local/clips/mlk他們將被重定向到http://example.local/clips/segment/index.php?clip=1

什麼實際上發生的是,當有人訪問example.local/clips/mlk他們被重定向到example.local/var/www/example/clips/segment/index.php?clip=1

我不確定它爲什麼這樣做。如果我改變重寫規則這樣:

RewriteRule ^mlk/?$ /segment/index.php?clip=1 [R=301,QSA,L] 

用戶被重定向到example.local /段/ index.php的夾= 1,這是仍然不正確?我不想在這些文件在網站目錄樹中移動的情況下指定絕對路徑。我怎樣才能讓這個工作相對而不是絕對?

回答

3

嘗試添加RewriteBase指令如下

RewriteEngine On 
RewriteBase /clips/ 

RewriteRule ^mlk/?$ segment/index.php?clip=1 [R=301,QSA,L] 

編輯

,但有沒有什麼辦法讓這對不使用RewriteBase指令

,你可以工作也請嘗試

RewriteEngine On 


RewriteCond %{REQUEST_URI} ^(/[^/]+/) [NC] 
RewriteRule ^mlk/?$ http://%{HTTP_HOST}%1segment/index.php?clip=1 [R=301,QSA,L] 
+1

RewriteBase指令的工作原理 - 但有沒有辦法讓這個工作,而不使用RewriteBase指令? – 2012-02-02 18:22:24

+0

我把條件改成了'RewriteCond%{REQUEST_URI} ^(/ [^ /] + /([^ /] + /)*)[NC]',它現在好像也適用於嵌套目錄。謝謝。 – 2012-02-08 20:10:20

+1

當希望規則獨立於目錄所在的名稱空間中的位置(以及深度)時,後者不起作用。 – 2016-04-16 16:40:46