2010-03-30 137 views
0

我有我找不出mod_rewrite重定向問題。重定向到子目錄和子目錄出

來自特定域的所有請求都會「靜靜地」重寫到指定的子目錄中。例如www.mydomain.net/hello.html檢索/net/hello.html中的文件。下面的.htaccess (放在我的託管根)達到這個完美:

RewriteEngine on 
RewriteBase/
RewriteCond %{ENV:REDIRECT_STATUS} 200 # <-- i will need this later. read to the end of the post. 
RewriteRule .* - [L] 
rewriteCond %{HTTP_HOST} ^www.mydomain.net$ 
rewriteCond %{REQUEST_URI} !^/net.*$ 
rewriteRule (.*) /net/$1 [L] 

但是,直接的網址進入該目錄但應明顯與301的URL重定向沒有該子目錄。例如www.mydomain.net/net/hello.html應該重定向到www.mydomain.net/hello.html(它仍然檢索/net/hello.html中的文件)。我.htacces此(放置在/net文件遺憾的是不起作用:

RewriteEngine on 
RewriteBase/
RewriteCond %{HTTP_HOST} ^localhost$ 
RewriteRule ^(.*) /$1 [R=301,L]  

我得到一個不定式重定向循環儘管在根.htaccess文件的RewriteCond %{ENV:REDIRECT_STATUS} 200塊...所以有什麼不對?

順便說一句,我必須使用mod_rewrite,因爲該網站是externaly託管,我沒有訪問的Apache配置。

非常感謝任何指針。

回答

1

檢查HTTP request lineTHE_REQUEST代替:

RewriteCond %{THE_REQUEST} ^GET\ /net[/? ] 
RewriteRule ^net($|/(.*)) /$2 [L,R=301] 
+0

感謝秋葵!使用THE_REQUEST規避循環。但是,我必須將RewriteRule行更改爲「RewriteRule ^(。*)/ $ 1 [L,R = 301]」,因爲.htaccess文件是從子目錄「net」執行的。 – halloleo 2010-04-12 06:06:41