Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTTP to HTTPS redirect not working with .htaccess

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]
like image 964
oznomal Avatar asked Jul 15 '18 21:07

oznomal


People also ask

How to redirect HTTP to HTTPS using htaccess file?

Now, you know how to edit the .htaccess file, let’s redirect HTTP to HTTPS using the edit .htaccess file method. 1. Redirect All Web Traffic Add this code below the existing code in your .htaccess file. 2. Redirect Only a Specific Domain Add this code to redirect a specific domain to use HTTPS. 3. Redirect Only a Specific Folder

How to redirect your website to https?

You can use .htaccess to redirect your site on HTTPS. What is .htaccess? Simply put, it is a configuration file. If you want to migrate to ‘https’ from ‘http’, you need to make a few changes in your .htaccess file. A .htaccess file comes with necessary instructions that can help you do that.

How to edit htaccess file remotely?

Method #2: Open the FTP program, and use the Edit function to edit the file remotely. Method #3: Go to cPanel and open the File Manager to edit the .htaccess file. (I have written this method in detail)

What is htaccess and how to use it?

A .htaccess file comes with necessary instructions that can help you do that. Therefore, .htaccess offers a way to make changes in the configurations on a per-directory basis. How to edit a .htaccess file?


2 Answers

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]
like image 198
Luay Avatar answered Oct 05 '22 01:10

Luay


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>
like image 38
Said Erraoudy Avatar answered Oct 05 '22 02:10

Said Erraoudy