How To Redirect HTTP to HTTPS Using .htaccess

If you have a secure (SSL, HTTPS) connection available for your website, even on another web host, it’s not a bad idea to redirect all of your visitors to the secure version of your website. This method can be achieved with multiple methods (if you’re using Cloudflare, there is another tutorial for that here), but this tutorial will show you how to redirect by adding code to your .htaccess.

If you are using 000webhost, you will need to setup with Cloudflare to get SSL.

How to redirect using .htaccess

  1. Open your website’s File Manager

  2. If you don’t already have one, create a .htaccess file.

  3. Edit the file, and paste the following code.

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Or this code if the previous one does not work for you

RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
  1. Save the file.

Success! You’ve just created a redirect to HTTPS! Now all of your visitors will see your content securely.