1

It seems like .htaccess doesn't redirect my pages from http to https. My domain is redirected to https but not my pages… Can it be a bad configuration from my web host?

Here is what I have done until now:

  • Choose a SSL certificate and installed it on my website from the host.
  • Force wp-config to pass from HTTP to HTTPs with: define('FORCE_SSL_ADMIN', true);
  • Tranform all of my URLs to HTTPs with Replace and Search DB
  • Add this domain to Google Search Console…

Put this code in my .htaccess:

<IfModule mod_rewrite.c>
RewriteEngine On
# Redirect to HTTPS 
RewriteCond %{HTTP_HOST} ^http://example\.com [NC]
RewriteCond %{SERVER_PORT} ^80$ [OR]
RewriteCond %{HTTPS} =off [OR]
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]
# Redirect from www HTTPS to HTTPS
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]

RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

I think that's all… Would you have an idea of what can cause this problem?

1 Answer 1

2
RewriteCond %{HTTP_HOST} ^http://example\.com [NC]

The HTTP_HOST variable contains just the hostname, not the scheme + hostname, so this will never match. If this never matches then it will never redirect http://example.com/... to HTTPS. (However, you have used this correctly in your second rule block which will redirect http://www.example.com/... to HTTPS.)

I assume you only have one domain? In which case, that directive should simply be removed (not corrected).

RewriteCond %{HTTPS} =off [OR]

However, you also have an erroneous OR flag on the last RewriteCond directive. If the preceding condition(s) matched then this would result in a redirect loop, since it's essentially <condition> OR true, which is always true.

Summary

# Redirect to HTTPS 
RewriteCond %{HTTP_HOST} ^http://example\.com [NC]
RewriteCond %{SERVER_PORT} ^80$ [OR]
RewriteCond %{HTTPS} =off [OR]
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]

So, in summary, you should replace the first rule block (above) with the following:

# Redirect to HTTPS
RewriteCond %{HTTPS} =off
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]

There's no point checking both SERVER_PORT and HTTPS. Either should be sufficient; not both.

1
  • 1
    Thank you very much, it work ! You are totally right, I am new to .htaccess and I didn't see this mistakes, even after several hours… Thank you also for your explanations. It's much clearer now. :)
    – Sandra
    Commented Jul 7, 2017 at 10:10

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.