htaccess to force www.

Is there a way with an .htaccess file to force a website to resolve to WWW?  For instance if someone typed in, https://MYSITE.com the .htacess file would force it to re-write to https://www.MYSITE.com?
LVL 7
rgranlundAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dave BaldwinFixer of ProblemsCommented:
There are several methods listed on this page: http://httpd.apache.org/docs/2.4/rewrite/remapping.html  Note that these only work if your SSL/TLS cert supports both with and without 'www'.  If it only works with the 'www', then it won't connect without it.
0
Zephyr ICTCloud ArchitectCommented:
Yes, something like this would do the trick normally:

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

Open in new window


Or an alternative:

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

Open in new window

0
rgranlundAuthor Commented:
Does it matter that it is HTTPS ?  (SSL)
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Dave BaldwinFixer of ProblemsCommented:
Yes, it does.  @spravtek's code needs to be changed to 'https' but also see my comment above.  Your certificate must include the domain name both with and without 'www' to connect to both And to redirect to the one you want.  If it only lists one version, then it won't allow a connection to the other and you can't redirect if you can't connect.
0
gheistCommented:
<Virtualhost ..:80.>
servername domain.com
Redirect / http://www.domain.com
</virtualhost
0
rgranlundAuthor Commented:
@gheist can you elaborate further?  I'm not really clear what you mean.
0
gheistCommented:
You create extra namevirtualhost

NameVirtualHost *:80

<Virtualhost *:80>
  servername www.domain.com
 all ye www.domain.com stuff here
</virtualhost>

<VirtualHost *:80>
 servername domain.com
 serveralias dept1.domain.com # add whatever hosts you have in DNS and on business cards
 Redirect permanent / http://www.domain.com
</VirtualHost>
0
Lucas BishopClick TrackerCommented:
The following htaccess code will assure that:
http://mysite.com
http://www.mysite.com
https://mysite.com

all redirect to:
https://www.mysite.com

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.mysite.com/$1 [L,R=301]
RewriteCond %{SERVER_PORT} 443
RewriteRule ^(.*)$ https://www.mysite.com/$1 [L,R=301]

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Apache Web Server

From novice to tech pro — start learning today.

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.