views:

36

answers:

2

This should be a very simple question to answer.

How would I setup a .htaccess file to send visitors from mydomain.com to www.mydomain.com?

I just had some Ajax headaches caused by the same-source rule, and this should be an easy way around it.

+3  A: 

Don't do that. Doing reverse makes sense. See http://no-www.org/

But to answer:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
Andrew Kolesnikov
`^(.*)$` Why capture?~
glebm
@Glex: that'll ensure all pages will always show up as www.example.com, not just the main one.See also: http://wiki.apache.org/httpd/CanonicalHostNames#Using_mod_rewrite, http://httpd.apache.org/docs/2.2/rewrite/rewrite_guide.html#canonicalhost
pioto
I checked out no-www. They didn't really explain why it's beneficial to use bare domian names, I'm supposed to take their word for it. People I talk to often treat sites without a www prefix with suspicion or confusion, so I'll stick with your answer. Thanks.
David Meister
Glex, Capture is there to redirect all urls, not just / like in your example (User lands through a bookmarked link deep within site, your code will fail, mine works).
Andrew Kolesnikov
A: 
RewriteEngine On
RewriteCond %{HTTP_HOST} ^host.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.host.com$
RewriteRule ^/?$ "http\:\/\/www\.host\.com\/" [R=301,L]
glebm
No need to escape "http\:\/\/www\.host\.com\/" like that, buddy.
Andrew Kolesnikov