7

I am trying to force my website to redirect http traffic to the https site and I have verified that the htaccess file is getting accessed but the redirect is not occurring. I have a valid cert for the site and the https version works fine but whether I type in www.mywebsite.com or http://www.mywebsite.com or website.com I still get taken to the HTTP version. Any suggestions?

.htaccess file

# Use PHP56
AddHandler application/x-httpd-php56 .php

# Always Redirect to HTTPS
RewriteEngine On
RewriteCond %{SERVER_PORT} !=443
RewriteCond %{HTTP_HOST} ^(www\.)?mywebsite\.com$ [NC]
RewriteRule ^$ https://mywebsite.com%{REQUEST_URI} [R=301,L]
Croises
  • 18,570
  • 4
  • 30
  • 47
oznomal
  • 439
  • 2
  • 8
  • 22

2 Answers2

7

I use this on my website and it redirects to HTTPS and not HTTP. There is also a great explanation here: Best Practice: 301 Redirect HTTP to HTTPS (Standard Domain)

RewriteEngine On

RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Luay
  • 789
  • 4
  • 15
  • 3
    I figured it out, I had my code snippet in the wrong htaccess file and this is why it wasn't triggering. I am still going to vote you correct though because your answer caused me to double check everything and figure out my mistake! – oznomal Jul 16 '18 at 01:22
  • The same happened to me. I was adding the rule in the root folder `.htaccess` of my hosting, but I had to add it to the `www` folder, in which wordpress is installed (in my case). Thanks! – Cristian Jan 19 '20 at 11:40
3

I have solved this problem by adding this code snippet, make sure it is at the beginning of the file .htaccess:

# Force HTTPS on all pages
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</IfModule>
Said Erraoudy
  • 1,490
  • 1
  • 13
  • 21