How to redirect HTTP to HTTPS using htaccess?

Please add the following code in your .htaccess file to automatically redirect visitors to the HTTPS version of your site:

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

The above code will redirect [http://yourdomain.com] to [https://yourdomain.com] and [http://www.yourdomain.com] to [https://www.yourdomain.com].

To force a www redirect, use the following code:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.yourdomain\.com$ [NC]
RewriteRule ^(.*)$ https://www.yourdomain.com/$1 [L,R=301]

The above code will redirect [http://yourdomain.com], [http://www.yourdomain.com], and [https://yourdomain.com] to [https://www.yourdomain.com].

If you have an existing .htaccess file:

  1. Do not duplicate RewriteEngine On.
  2. Make sure the lines beginning RewriteCond and RewriteRule immediately follow the already-existing RewriteEngine On.
  • 434 Users Found This Useful
Was this answer helpful?

Related Articles

How to generate a CSR (Certificate Signing Request)?

Please follow this walkthrough to generate a CSR:   01. Setup an email address...

How to order a dedicated IP?

You can order a dedicated IP from your Client Area: Log into your Client Area. Go to Services...

How to prevent IP addresses from getting blacklisted?

We use MailChannels to use high-reputation IPs to send emails to avoid blacklisted IP issues....

Malware Scanner - what do you offer?

We use Imunify360 or ConfigServer eXploit Scanner (CXS) to protect comprehensively against a...

How to install SSL certificates on domains using Cloudflare?

You can easily install SSL certificates on your hosting without exposing the server IP when you...