Home > front end >  How to force www and https using htaccess rules
How to force www and https using htaccess rules

Time:01-23

I have a WordPress website and am trying to force www.example.com using htaccess rules. The WP general settings are https://www.example.com and my CDN host header is set to www.example.com. The problem is I can still enter https://example.com in Firefox to bring up the site. If I enter just example.com in the browser it renders as www.example.com.

Note: There is one subdomain for this site that cannot use www.

I added the following rules to my htaccess file but they are not working:

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteCond %{HTTPS} !=on [NC]
    RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteRule (.*) https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>

CodePudding user response:

According to a2hosting, replacing what you have above with the following should do the trick:

# Add www to any URLs that do not have them:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

CodePudding user response:

The solution was to change a rule in my firewall to force www. I learned the hard way that firewall rules were overriding CDN rules.

  • Related