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
  • 620,600
  • 104
  • 758
  • 828
Allan
  • 2,526
  • 5
  • 25
  • 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
  • 620,600
  • 104
  • 758
  • 828
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,034
  • 7
  • 58
  • 76
  • 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