2017-02-03 67 views
1

我想重定向example.com/testwww.example.com/testexample.com/contactwww.example.com/contacthtaccess重定向非www到www全路徑

因此,所有的網絡路線。 的一切,我覺得,他們做的是重定向到example.com/testwww.example.com

下面是我的代碼,並與所有的,我已經嘗試過了發生在我身上一樣。他們工作得很好,但他們沒有做我想做的事。

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

請任何建議。 謝謝!

+1

的[重定向的.htaccess非www到www保留URI字符串]可能的複製(http://stackoverflow.com/questions/1685962/ htaccess的重定向的非www到WWW保留-URI字符串) – Walf

回答

1
//Rewrite to www 
Options +FollowSymLinks 
RewriteEngine on 
RewriteCond %{HTTP_HOST} ^example.com [nc] 
RewriteRule ^(.*)$ http://www.example.com/$1 [r=301,nc] 
1

嘗試下面的規則,

RewriteEngine on 
RewriteCond %{HTTP_HOST} !^www 
RewriteRule^http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L] 
1

方法我

RewriteEngine On 
RewriteBase/
RewriteCond %{SERVER_NAME} !^www\. 
RewriteRule ^(.*)$ http://www.%{SERVER_NAME}/$1 [R,NC,L] 

方法II

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

您可以使用這兩個。兩者之間的唯一區別是%{SERVER_NAME}%{HTTP_HOST}

希望它有幫助!