2012-10-01 29 views
0

的WWW版本在我的主要.htaccess文件在我的域的根,我有以下代碼:應用的.htaccess重定向到所有子目錄現場

RewriteEngine on 
# If missing 'www' 
RewriteCond %{http_host} ^example.com [nc] 
# Redirect to 'www' version 
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,nc] 
# Remove 'index.php' from URL 
RewriteRule ^index.php$ http://www.example.com/ [R=301,nc] 

然後我有一個單獨的.htaccess文件中的每個目錄,還刪除URL中的index.php,如下所示的/products目錄:

RewriteEngine on 
RewriteRule ^index.php$ http://www.example.com/products/ [r=301,nc] 

當我(我清除緩存後)參觀example.com我重定向到如預期的那樣。

但是,如果我在地址欄中輸入example.com/products,頁面加載的example.com/products/和我重定向到www.example.com/products/版本。我究竟做錯了什麼?請幫助...並且如有必要,我可以提供更好的解釋/示例。

回答

1

當您打開在一個子目錄一個htaccess重寫引擎,它排除了可能在htaccess的文件,其任何父目錄的,除非你使用的所有重寫規則:在

RewriteOptions inherit 

指令htaccess文件在你的子目錄中。由於/products中唯一的規則只有index.php的重定向,除非請求是/products/index.php,否則不會應用任何規則,因爲父目錄中的規則會被忽略。

另請注意,在Apache 2.2中,inherit選項將位於子目錄中的規則放置在之後的父級重寫規則


編輯:

繼承的規則不起作用,因爲該基地是不一樣的。您只需要在其他地方將重定向添加到www規則中。因此,在您/products/目錄添加這些到您的htaccess文件:

RewriteCond %{http_host} ^example.com [nc] 
RewriteRule ^(.*)$ http://www.example.com/products/$1 [R=301,nc] 

或者,你可以保持RewriteOptions inherit但是從你的文檔根改變你的htaccess規則:

RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,nc] 

到:

RewriteRule ^(.*)$ http://www.example.com%{REQUEST_URI} [R=301,nc] 
+0

好的...所以如果我想要根目錄和子目錄中的所有URL重定向到他們的www版本,並且我想從所有的URL中刪除'index.php',我應該把什麼放在'/ products/.htaccess'中? – adamdehaven

+0

@AdamD正是我在回答中所說的。 'RewriteOptions繼承'。這不夠清楚嗎? –

+0

噢好吧...所以我只想'RewriteOptions繼承'並刪除其他所有內容? (只是想確保b/c網站目前正在運行):) – adamdehaven

相關問題