Redirect non-www to www in .htaccess

Doing some Rewrites in the .htaccess file for a website that I am actively doing SEO on. I want to ensure I have just one version indexed in Google and it seems like all of my backlinks are pointed to

http://akenterprisesroofing.ca

I do have some code in the .htaccess file now and it does redirect http://www.akenterprisesroofing.ca to the shorter version www.akenterprisesroofing.ca

I want to know how to also include the redirect to get http://akenterprisesroofing.ca to the URL www.akenterprisesroofing.ca as well.

this is my code

RewriteEngine On
RewriteRule ^index\.html$ / [R=301,L]
RewriteRule ^(.*)/index\.html$ /$1/ [R=301,L]

Open in new window

fadercreepAsked:
Who is Participating?
 
InsoftserviceCommented:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
0
 
fadercreepAuthor Commented:
Also the above code redirects http://www.akenterprisesroofing.ca/index.html to the short version www.akenterprisesroofing.ca

just need it to go the other way? Any Thoughts on how to get akenterprisesroofing.ca to redirect to www.akenterprisesroofing.ca
0
 
fadercreepAuthor Commented:
Also tried this code with the same result.. is there any other place where I can miss setting this?

Options +FollowSymLinks RewriteEngine on
RewriteCond %{HTTP_HOST} ^akenterprisesroofing\.ca
RewriteRule ^(.*)$ http://www.akenterprisesroofing.ca/$1 [R=permanent,L]

Open in new window

0
 
Steve BinkCommented:
Your question is a little confusing - you state that the "short" version is "www.akenterprisesroofing.ca", and you want to redirect from the same URL.  

The last bit of code you posted is almost what you need.  Below is a minor edit, but it should otherwise work.  If it is still not redirecting, turn on your RewriteLog and post an excerpt showing what is happening.
Options +FollowSymLinks 
RewriteEngine on
RewriteCond %{HTTP_HOST} ^akenterprisesroofing\.ca
RewriteRule ^/?(.*)$ http://www.akenterprisesroofing.ca/$1 [R=permanent,L]

Open in new window

0
 
arnoldCommented:
What is the difference that you are seeing?
How is the VirtualHost entry setup, is it setup with the servername as www.akenterprisesroofing.com with a serveralias as akenterprisesroofing.com?
if it is not, i.e. it is setup in the opposite order, change the references as suggested above and see whether that achieves what you want.

The redirect is unnecessary and resource wasting.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.