Apache: How to set your domain to redirect to WWW by default
Add the below to your .htaccess file in order to have your-domain redirect to www.your-domain. Just make sure where it says “your-domain.com” to fill in your domain on both lines 2 and 3.
RewriteEngine on RewriteCond %{HTTP_HOST} !^www.you-domain.com$ RewriteRule ^(.*)$ http://www.your-domain.com/$1 [R=301] |
Why would you do this? It makes your site nice and clean and loading off www.your-domain.com. In addition this avoids the duplicate content penalty from search engines, because otherwise your site appears to have a site at “www.yoursite.com” and at “yoursite.com”.
Al October 22, 2012 at 12:40 am
Perfect. Thank you for this. But I have one question though. In Firefox and Chrome they both show http://www.mydomain.com but in IE it shows http://www.mydomain.com/index.php. How can I make IE show the same. Thanks again.
Al Guevara September 6, 2013 at 9:29 pm
So does this mean one cannot use a non www domain for google page speed?
IE:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^you-domain.com$
RewriteRule ^(.*)$ http://your-domain.com/$1 [R=301]
John Oliver at Site5 September 10, 2013 at 4:08 pm
Hello Al,
I hope this reply finds you well!
You are correct. The non-www based domain cannot be pointed at the Google PageSpeed CNAME and in turn all non-www based traffic would need to be redirected to your www based domain in order for that traffic to be routed through Google PageSpeed.
The htaccess file in this article (located above) will help you redirect the traffic from your non-www based domain to your www domain.
If you have any questions, please feel free to leave a comment here, or if you prefer, you can open a ticket with our support department through Backstage.
Happy Hosting!
Nicolas May 13, 2016 at 12:04 am
Great, I had the problem in this but now it resolved, thanks team site5 :)
Corey Mahon May 25, 2016 at 9:54 am
Hi Nicolas!
Fantastic! You’re welcome and glad to hear that your problem was resolved!
Jon McK July 14, 2016 at 3:08 pm
When using this, public_html is inserted into the URL (presumably related to running a rails app). Any suggestions?
domain.com/path/to/content
redirects to
domain.com/public_html/path/to/content
which returns a 404
Corey Mahon July 18, 2016 at 12:51 pm
Hi Jon!
That is an odd issue indeed and it does sound as if the redirect rules need a bit of tweaking to make them work properly. If you would please open a ticket through your backstage panel, our team will be glad to take a look and find a working combination of redirect rules for you!