1

Redirecting a visitor who hits http://example.com to http://www.example.com isn't terribly difficult. But how is it done in conjunction with a RewriteRule that directs all page requests through "index.php"?

RewriteRule !\.(gif|jpg|png|css|js|php|ico|xml)$ /index.php
Gumbo
  • 643,351
  • 109
  • 780
  • 844
Allan
  • 2,586
  • 5
  • 26
  • 22

2 Answers2

5

You just need to make sure that those rule, that cause an external redirect, appear before those, that cause internal rewrites. So simply:

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
Gumbo
  • 643,351
  • 109
  • 780
  • 844
1

See the answer for this post, just do the opposite.

<VirtualHost *:80>
    ServerName example.com/
    RedirectPermanent / http://www.example.com/
</VirtualHost>
Community
  • 1
  • 1
TJ L
  • 23,914
  • 7
  • 59
  • 77
  • 1
    I was just writing something similar. I like this way because the re-directing from non-www doesn't interfere with your 'proper' virtual host at all. – SpoonMeiser Jul 17 '09 at 16:20