I have a site that I have redirecting to HTTPS. I do this to leverage wildcard SSL for my password protected pages. Everything seems to work fine with testing. For example, whether you type in HTTP or www, you always get redirected to the SSL HTTPS...

That said, I have about 200-300 external backlinks -- many high quality, yet Google Webmaster (along with SEOMoz), shows I have just 4... Huh? I'm embarrassed to say I just discovered this. This has led me to hypothesize that maybe my settings in .htaccess is messed up, so Google isn't recognizing a link because it's recorded on another site as HTTP, instead of HTTPS. Maybe? At any rate, here is my simple .htaccess setting for 301 www to HTTP, and from HTTP to HTTPS.

RewriteCond %{SERVER_PORT} !443
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]

RewriteCond %{SERVER_PORT} 443
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]

Like I said, everything works fine for redirect over HTTPS, so I'd rather not screw up what works. On the other hand something is very wrong with Google finding all my back links, so I need to fix something... I'm just wondering that maybe Google isn't picking up a my backlinks from other websites recording me as HTTP because I'm at HTTPS. Maybe Google doesn't care and it's some other issue. Am I barking up the right tree? If so any quick fixes?

  • 1
    "whether you type in http or www, you always get redirected to the SSL https" - What you have posted does not appear to do this? This code simply redirects to the non-www version of the domain, but keeps the protocol as HTTP or HTTPS. I can't see how it redirects from HTTP to HTTPS?
    – MrWhite
    Commented Dec 14, 2012 at 22:43
  • Thanks, the second rule above redirects to https. And it does work. But it sounds like you think this should be written differently, perhaps in a way that doesnt confuse google. Any suggestions? Again, i'm looking for a redirect that pushes everyone to https. That includes those that link to http and www, which is what this htaccess rule was trying to accomplish. Thoughts?
    – user20680
    Commented Dec 14, 2012 at 23:38
  • "the second rule above redirects to https" - yes, but it redirects from HTTPS (ie. port 443), it doesn't redirect from HTTP to HTTPS.
    – MrWhite
    Commented Jan 30, 2019 at 18:15

1 Answer 1


These 301s seem to have many ways of solving this and it can get confusing. I had a special use-case trying to do two things in one shot >> redirect all http://www.example.com & http://example.com over to https://example.com.

This worked like a charm for me. These two conditions play nicely.

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

RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
  • These directives don't do what you say they do... they don't redirect HTTP to HTTPS. They simply remove the www subdomain and maintain the same protocol - in this sense they are no different to the directives posted in the question. These directives won't do anything for http://example.com (as stated above) since the second condition on both rules states that the Host must start www. If the site is redirecting HTTP to HTTPS then something else is doing this.
    – MrWhite
    Commented Jan 30, 2019 at 18:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.