HTTP redirect to HTTPS from .htaccess

Here’s another quick guide! This time on how to redirect HTTP traffic to HTTPS from your .htaccess file.

It’ll cover how to:

  • Redirect all traffic from HTTP to HTTPS
  • Redirect specific domain from HTTP to HTTPS
  • Redirect specific folder from HTTP to HTTPS
  • Redirect all traffic, except certain file(s), from HTTP to HTTPS

Before we dig into the code. Please make sure to create the .htaccess file, if not yet created. This file should be located at the root of your website, eg. the public_html folder.

Redirect all traffic from HTTP to HTTPS

RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.YOURDOMAINNAME.com/$1 [R,L]

Redirect specific domain from HTTP to HTTPS

RewriteEngine On
RewriteCond %{HTTP_HOST} ^YOURDOMAINNAME\.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.YOURDOMAINNAME.com/$1 [R,L]

Redirect specific folder from HTTP to HTTPS

RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} foldername
RewriteRule ^(.*)$ https://www.YOURDOMAINNAME.com/foldername/$1 [R,L]

Redirect all traffic, except certain file(s), from HTTP to HTTPS

RewriteEngine On
RewriteCond %{SERVER_PORT} 80 
RewriteCond %{REQUEST_URI} !^/FOLDERNAME/file.xml$ [NC]
RewriteRule ^(.*)$ https://www.YOURDOMAINNAME.com/$1 [R=301,L]

You may specify several files, to not have them redirected to HTTPS.

Over’n’Out!

Add a Comment

Your email address will not be published.