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, the .htacess file would force it to re-write to
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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:  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.
Zephyr ICTCloud ArchitectCommented:
Yes, something like this would do the trick normally:

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

Open in new window

Or an alternative:

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

Open in new window

rgranlundAuthor Commented:
Does it matter that it is HTTPS ?  (SSL)
10 Tips to Protect Your Business from Ransomware

Did you know that ransomware is the most widespread, destructive malware in the world today? It accounts for 39% of all security breaches, with ransomware gangsters projected to make $11.5B in profits from online extortion by 2019.

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.
<Virtualhost ..:80.>
Redirect /
rgranlundAuthor Commented:
@gheist can you elaborate further?  I'm not really clear what you mean.
You create extra namevirtualhost

NameVirtualHost *:80

<Virtualhost *:80>
 all ye stuff here

<VirtualHost *:80>
 serveralias # add whatever hosts you have in DNS and on business cards
 Redirect permanent /
Lucas BishopClick TrackerCommented:
The following htaccess code will assure that:

all redirect to:

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

Open in new window

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.