2

This question has been asked a million times before but my problem is that this still doesn't work for me, regardless of what code I use so I was wondering if I could have some help perhaps?

This is my current htaccess and it does successfully remove the www, but the https doesn't work at all. I want it to redirect all traffic to https://example.com if the requested url uses www or http. Any help please? Thanks a bunch!

RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} !^example.com
RewriteRule ^(.*)$ https://example.com/$ [L,R=301]
  • note: the rewrite engine is on, it's a few lines above.
7
  • Does the site load if you try to load it over SSL?
    – John Conde
    Feb 16, 2015 at 21:30
  • Yep, ssl does work so if you put in the https to begin with it still works, it's just the auto redirects that don't.
    – user49815
    Feb 16, 2015 at 21:31
  • After some inspection, it looks like the {HTTPS} off isn't working. That's what is causing the loop. But what do I use instead? :(
    – user49815
    Feb 16, 2015 at 21:46
  • 1
    Your HTTPS syntax should be RewriteCond %{HTTPS} !=on [OR] (you are missing the equals sign and either it or the other rule should trigger the redirect). Feb 16, 2015 at 22:07
  • 1
    You're last $ should also be $1 to substitute everything in the parenthesis. Feb 16, 2015 at 22:33

3 Answers 3

5

Use this. It should save you from two headaches.

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

this also allows any URL that starts with http://www.example.com/ or that connects to port 80 (the standard web port) to redirect to https://example.com/

5
  • Well I tried the port option instead. It could just be cached even though I've cleared it but using port works in Chrome but doesn't work in Safari. I've also asked friends who say that it doesn't work (one tried it in Chrome and IE).
    – user49815
    Feb 16, 2015 at 22:30
  • It sounds like a cache issue if it isn't working in a specific browser. Use the command line to test these. I like cURL: curl --head 'http://example.com/' Feb 16, 2015 at 22:34
  • What should that come back as? I used it in terminal and this is what I got... HTTP/1.1 200 OK Date: Mon, 16 Feb 2015 22:39:15 GMT Server: Apache X-Powered-By: PHP/5.3.28 Connection: close Content-Type: text/html Set-Cookie: DYNSRV=lin215; path=/ Cache-control: private It's also worth noting I did curl --head https and http and they came back with the above.
    – user49815
    Feb 16, 2015 at 22:47
  • if its not what you want it to point to, then the headers should return data that starts with HTTP/1.1 301 to indicate a redirect and also a location header pointing to the https version of the site. Feb 16, 2015 at 23:34
  • It should be SERVER_PORT, not HTTP_PORT.
    – MrWhite
    Feb 17, 2015 at 1:07
1

You can also use the following:

RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^domain-name.tld$ [NC]
RewriteRule ^(.*)$ https://domain-name.tld/$1 [L,R=301]
1
  • If this answered your question then you can mark it as "accepted" by clicking the tick next to the answer (which takes the question out of the unanswered question queue). Once you have 15 rep you can also upvote answers that you find helpful. Thanks.
    – itteam
    Dec 29, 2015 at 16:42
1

I've tried many different configs and rules, the following is the only bulletproof solution that works for me.

    # match any URL with www and rewrite it to https without the www
    RewriteCond %{HTTP_HOST} ^(www\.)(.*) [NC]
    RewriteRule (.*) https://%2%{REQUEST_URI} [R=301,L]

    # match non https and redirect to https
    RewriteCond %{HTTP:X-Forwarded-Proto} !https
    RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]

The order matters, as it will prevent a 3rd redirect in some cases.

For a subdomain, you just use

    # match non https and redirect to https
    RewriteCond %{HTTP:X-Forwarded-Proto} !https
    RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]

I use Cloudways coupled with Cloudflare and this works perfectly, all other provided examples caused a redirect loop or failed to resolve..

Your Answer

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

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