Page 1 of 1

Best way to redirect example.com to www.example.com

Posted: Thu Jun 07, 2007 7:43 pm
by Luke
I've got a site where the secure certificate is set to http://www.example.com and not example.com. How would you recommend redirecting example.com to http://www.example.com so that the browser doesn't whine about a domain mismatch? Redirect directive? mod_rewrite?

EDIT: sorry, the server runs apache - not sure what version

Posted: Thu Jun 07, 2007 7:48 pm
by Christopher
Take a look at ServerAlias.

Posted: Thu Jun 07, 2007 7:53 pm
by Luke
I'm on a shared host. I don't think I can use that. :(

Posted: Thu Jun 07, 2007 7:59 pm
by Christopher
Even with a virtual host you can usually set the ServerName to http://www.example.com and the ServerAlias to example.com.

Posted: Thu Jun 07, 2007 9:32 pm
by Arawn
In Apache 1.3 ServerAlias is only available in Vertual Host and ServerName is available in Virtual Host and Server Config.

Code: Select all

RewriteEngine on
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

# You don't need this but ...
RewriteRule ^$ /index.html [R=301,L]
I still have hosts that are using 1.x versions of Apache.

Posted: Fri Jun 08, 2007 9:45 am
by pickle
A redirect directive is better than mod_rewrite (and arguably a server alias - depending on how it works). With a redirect, the user will be informed of the changed URL, and after the move, the server won't have to run a rewrite on every request.