Avatar of asianandrew
asianandrewFlag for United States of America

asked on 

How to force www and https

I am trying to force a site to use SSL via HTTPS and have the www.domain.com.

I have successfully forced an HTTPS connection with the following in my .htaccess file:

RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]

How can I modify this to check and force for a www as well?

Thanks
Apache Web Server

Avatar of undefined
Last Comment
asianandrew
Avatar of TK-77
TK-77
Flag of United States of America image

Try this:

# Auto add "www" in front of domain name
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain.com
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=permanent,L]

TK
Avatar of asianandrew
asianandrew
Flag of United States of America image

ASKER

Close, but if I go to https://domain.com, it doesn't forward to https://www.domain.com

Thanks
ASKER CERTIFIED SOLUTION
Avatar of TK-77
TK-77
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of asianandrew
asianandrew
Flag of United States of America image

ASKER

May want to include the full solution in the future to clarify for future viewers. Thanks for your help.
Avatar of asianandrew
asianandrew
Flag of United States of America image

ASKER

Just for clarification - the following was placed in my .htaccess file for the particular domain I was working with:

RewriteEngine On
# Forces HTTPS requests (SSL)
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
# Forces the site to use www
RewriteCond %{HTTP_HOST} ^mydomain.com
RewriteRule ^(.*)$ https://www.mydomain.com/$1 [R=permanent,L]


Apache Web Server
Apache Web Server

The Apache HTTP Server is a secure, efficient and extensible server that provides HTTP services in sync with the current HTTP standards. Typically Apache is run on a Unix-like operating system, but it is available for a wide variety of operating systems, including Linux, Novell NetWare, Mac OS-X and Windows. Released under the Apache License, Apache is open-source software.

21K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo