If, for some reason, you can't deal with it at the DNS level, you can at least deal with it at the server level. PHP is not the greatest tool for something like this (as the previous poster mentioned). If you are on Apache and you have mod_rewrite, you can add an .htaccess file and put this in it:
Code: Select all
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
RewriteRule (.*) http://www.example.com/$1 [R,L]
Line 1 - turns on the mod_rewrite engine in apache.
Line 2 - basically says "if the host name entered was anything BUT
http://www.example.com, apply the next line". [NC] means it is
Not
Case sensitive
Line 3 - basically says "rewrite any URL to
http://www.example.com/whatever_url_was_entered" [R,L] means that it is a
Redirect (R) and that it if the rule matches, don't continue matching with any of the other rewrite rules that may be in the file. It is the
Last rule. (L).
This will work as long as you don't have other subdomains that you need to work. If you do have other subdomains you will have to do something like this instead:
Code: Select all
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www|accounts|webmail).example.com$ [NC]
RewriteRule (.*) http://www.example.com/$1 [R,L]
In this one, basically it is saying that all domains but
http://www.example.com, accounts.example.com, and webmail.example.com should be redirected to
http://www.example.com.
Now you may be wondering, "why bother putting the www in there? It would be OK to redirect
http://www.example.com to
http://www.example.com. What will that hurt?" Well... it will send the server into a never-ending loop. So that's why

Hope this helps!