HTTP to HTTPS Redirect using .htaccess Apache Web Config

In order to force your website traffic to use HTTPS and not use HTTP, we have to add or edit RewriteEngine and RewriteCond Apache Web Server or NGINX Web Server and many other web servers that use .htaccess file. .htaccess file is used to specify the Web Server configurations at the developer side when you do not have access to httpd.conf file.

Redirecting HTTP to HTTPS

1. Redirect All Web Traffic

If you have existing code in your .htaccess, add the following:

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

2. Redirect Only a Specific Domain

For redirecting a specific domain to use HTTPS, add the following:

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

3. Redirect Only a Specific Folder

Redirecting to HTTPS on a specific folder, add the following:

RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} folder
RewriteRule ^(.*)$ https://www.your-website-domain.com/folder/$1 [R,L]

Note: Replace “your-website-domain” with your actual domain name wherever required. Also, in case of the folder, replace /folder with the actual folder name.

Think it was helpful? Share this article to help others come on HTTPS.

Example: I have to Redirect my domain rgvp.in http:// traffic to https://. I will write the following Code.

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