3

I've got an ecommerce site where we're have some reports (random) of the cart not operating correctly. I think it might be due to removing www not working with https

my current htaccess code looks like this:

# remove the www
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.domain.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [R=301,L]

What do I need to do to also force this on https?

CreateSean
  • 133
  • 1
  • 5

1 Answers1

8

This looks like the same situation covered on StackOverflow: URL rewriting for different protocols in .htaccess

Here is the code for removing the www from your hosts, regardless of HTTP or HTTPS protocol:

RewriteCond %{HTTP_HOST} ^www\.
RewriteCond %{HTTPS}s ^on(s)|off
RewriteCond http%1://%{HTTP_HOST} ^(https?://)(www\.)?(.+)$
RewriteRule ^ %1%3%{REQUEST_URI} [R=301,L]

If you want to remove www and redirect to HTTPS the rule is much simpler. See: What are the most commonly used and basic Apache htaccess redirects?

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ https://example.com$1 [R=301,L]
Stephen Ostermiller
  • 99,822
  • 18
  • 143
  • 364