2009-11-06 119 views
9

我運行笨平臺,它使用的.htaccess接受像的.htaccess重定向非www到www保留URI字符串

http://www.mysite.com/controller/function/argument 

網址,我目前使用的一些重寫的​​.htaccess,即(簡體):

RewriteEngine On 
RewriteCond %{REQUEST_URI} !^(/index\.php|/images|/assets) 
RewriteRule ^(.*)$ /index.php/$1 [L] 

我想添加一個重寫規則,將所有非www請求重定向到www。我還希望域名後面的URI字符串在重定向中保持不變。例如,如果用戶請求http://mysite.com/controller/function/argument,我希望.htaccess文件在瀏覽器中將請求重寫爲http://www.mysite.com/controller/function/argument,然後處理該請求。

回答

10

應該是這樣的:

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

這不會干擾我的第二個RewriteRule吧?我瞭解你的RewriteCond,我不想適用於我已有的規則。 – 2009-11-06 08:14:04

+0

如果您將我的地址添加爲第一個地址,則只有在主機不是www.mysite.com時纔會激活它。如果它被激活,將會有一個重定向的HTTP,第二個HTTP查詢將激活你自己的重寫,如果條件符合 – rossoft 2009-11-06 08:36:58

+0

在這裏查看更多信息: http://www.webconfs.com/how-to-redirect- a-webpage.php – rossoft 2009-11-06 08:39:07

14

我也有類似的問題,而這個的.htaccess對我的作品

RewriteEngine On       

#This bit rewrites your host name to include www 
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC] 
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,NC,L] 

#This bit does the codeigniter magic 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ /index.php/$1 [L] 
+0

它正在工作。如果我想添加除blog.example.com之外的規則未被重定向到www? – Permana 2013-07-08 07:16:46

+0

嘗試'RewriteCond%{HTTP_HOST}!^(www | blog)\。example \ .com [NC]' – ekhaled 2013-08-23 11:42:45

0

如果你是在Apache 2.4,你可以使用下面的簡單重定向:

<if "%{HTTP_HOST} =='example.com'"> 
Redirect/http://www.example.com/ 
</if>