How to redirect all HTTP requests to HTTPS using .htaccess rules?

.htaccess, http, https, redirect, security

Solution

Add the following inside your `.htaccess` file

RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

Problem

I'm trying to redirect all insecure HTTP requests on my site (e.g. `http://www.example.com`) to HTTPS (`https://www.example.com`). How can I do this in .htaccess file? By the way, I'm using PHP.

Original source

Related problems