HTTPS Htaccess Redirect

Automatically redirect to https from .htaccess

For those who have a website running on an Apache server and have a secure layer through a certificate (SSL), you can automatically redirect all your visitors to a secure version of your website over HTTPS.

Being able to have a 100% secure website running on a secure connection is no longer a minor issue these days, and it’s no longer a problem only for websites that handle money transactions. Soon, search engines will start penalizing sites that don’t have this configuration enabled.

Currently, browsers already alert their users about websites that don’t meet these security requirements, which could result in a drop in visits to your site. Users, fearing being deceived, may leave your site upon seeing these messages in their browsers.

Let's get straight to the point

To achieve this, you need to either create a .htaccess file or edit an existing one located in the root directory where you want to apply this configuration. If you edit it in the parent directory, this configuration will apply to the entire site.

Use the following code to automatically redirect all your visitors to the HTTPS version of your site:

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

If you already have a .htaccess file, keep the following in mind:

  • Don’t duplicate the RewriteEngine On. One instance is enough.
  • Make sure that the lines containing RewriteCond and RewriteRule are written immediately after the existing RewriteEngine On.

With this configuration active, your site will be HTTPS!

Important: Keep in mind that the custom code on your site or even specific server configurations where your site is hosted might cause issues with the method described above. If you have questions, consult your website administrator.

See you next time!

Blog details

  • Date: 20/12/2024
  • Category: Development
  • Author: Sebastián Torres