2015-05-12 96 views
1

我使用.htaccess在我的opencart上使用SEO URL,現在我想用www重定向所有非www URL訪問URL。所以,我在.htaccess文件中添加這些行:重定向非www到www添加URL查詢字符串

RewriteCond %{HTTP_HOST} !^www\. 
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L] 

是現在的URL總是重定向到www如果我的域名之前寫wihout WWW,但我發現,我認爲它不應該出現在什麼URL。

當我去www.mydomain.com/category我試圖通過刪除www(所以它變成了mydomain.com/category)並點擊ENTER來測試該URL。該網址被重定向到www.mydomain.com/index.php?_route_=category。我不知道是什麼讓網址變成這樣,我認爲該網址應該重定向到www.mydomain.com/category。我曾嘗試這一行前後移動的重定向線位置:

RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA] 

以下是完整的.htaccess內容:

RewriteBase/

RewriteRule ^sitemap.xml$ index.php?route=feed/google_sitemap [L] 
RewriteRule ^googlebase.xml$ index.php?route=feed/google_base [L] 
RewriteRule ^download/(.*) /index.php?route=error/not_found [L] 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_URI} !.*\.(ico|gif|jpg|jpeg|png|js|css) 
RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA] 

# Redirect non-www to www (I have tried to move this lines but still resulting the same) 
RewriteCond %{HTTP_HOST} !^www\. 
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L] 

什麼是錯我的重定向行?任何幫助和建議,將不勝感激。謝謝。

回答

1

問題是您已經將域的重寫過低。將它移動到剛RewriteBase /後,它會執行前的URL重寫做

RewriteBase/
# Redirect non-www to www (I have tried to move this lines but still resulting the same) 
RewriteCond %{HTTP_HOST} !^www\. 
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L] 

RewriteRule ^sitemap.xml$ index.php?route=feed/google_sitemap [L] 
RewriteRule ^googlebase.xml$ index.php?route=feed/google_base [L] 
RewriteRule ^download/(.*) /index.php?route=error/not_found [L] 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_URI} !.*\.(ico|gif|jpg|jpeg|png|js|css) 
RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA] 
+0

是的,我也這麼認爲,我已經把域名重定向線下方'RewriteBase /'如你所說,但URL不斷當我將「www.mydomain.com/categoryname」更改爲「mydomain.com/categoryname」時,更改爲「www.mydomain.com/index.php?_route_ = categoryname」。 – Eaton

+0

如果這仍在發生,我懷疑你的瀏覽器正在緩存它。清除你的整個瀏覽器緩存,看看是否能修復它 –

+0

我希望如此,以後我會告訴你它是否有效。 – Eaton