2009-07-17 139 views
1

重定向誰打http://example.comhttp://www.example.com並不是十分困難的訪問者。但是,它是如何與通過「index.php」引導所有頁面請求的RewriteRule一起完成的呢?重定向非www網址到www網址連同其他規則

RewriteRule !\.(gif|jpg|png|css|js|php|ico|xml)$ /index.php 

回答

5

您只需要確保那些導致外部重定向的規則出現在導致內部重寫的那些規則之前。簡而言之:

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

RewriteRule !\.(gif|jpg|png|css|js|php|ico|xml)$ /index.php 
1

請參閱答案for this post,正好相反。

<VirtualHost *:80> 
    ServerName example.com/ 
    RedirectPermanent/http://www.example.com/ 
</VirtualHost> 
+1

我只是寫了類似的東西。我喜歡這種方式,因爲從非www的重定向不會干擾你的'適當的'虛擬主機。 – SpoonMeiser 2009-07-17 16:20:39