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
Best way to redirect example.com to www.example.com
Moderator: General Moderators
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Even with a virtual host you can usually set the ServerName to http://www.example.com and the ServerAlias to example.com.
(#10850)
In Apache 1.3 ServerAlias is only available in Vertual Host and ServerName is available in Virtual Host and Server Config.
I still have hosts that are using 1.x versions of Apache.
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]
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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.