Auto-redirecting to www.domain

DReade83

Well-Known Member
Oct 20, 2006
196
0
166
Cheshire, UK
How can I automatically redirect users to www.mydomain.com who visit mydomain.com (without the www)?

I could use PHP code:
PHP:
if(!ereg("www", $_SERVER['HTTP_HOST'])) {
   header("Location: http://www.mydomain.com/"); exit();
}
But this is a little fiddly. Is there an easier was of doing it?
 

Spiral

BANNED
Jun 24, 2005
2,018
8
193
Instead of using PHP code in an index script, you can just simply add
mod_rewrite commands to your .htaccess to re-write urls to use "www" ...