How to Redirect HTTP to HTTPS using .htaccess file

redirect http to https using htaccess

Converting your website from HTTP to HTTPS makes it more secure and also improves its credibility. This also improves its search engine rankings and makes it more visible. However, once you make your website accessible over HTTPS you need to ensure that your old HTTP URLs still work. You can do this by redirecting your HTTP URLs to HTTPs using .htaccess file on your server. Here’s how you can redirect HTTP to HTTPS using .htaccess file.


Redirect HTTP to HTTPS using .htaccess file

Basically, you need to open .htaccess file using your preferred text editor and paste the following commands, depending on your requirement. This will effectively force HTTPS on your website. Listed below are some of the common uses cases for redirecting HTTP to HTTPS.



Redirect All Web Traffic

If you want to force all web pages to use HTTPS, copy+paste the following commands in your .htaccess file

RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$$1 [R,L]


Redirect only a specific domain to HTTPS

Sometimes you may need to redirect only a specific domain such as or, instead of all pages. In such cases, you can use the following commands. The following example redirects all pages in to their HTTPS versions

RewriteEngine on
RewriteCond %{SERVER_PORT} ^80$
RewriteCond %{HTTP_HOST} ^app\.yourdomain\.com$ [NC]
RewriteRule .? https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]


Redirect only a specific folder to HTTPS

In case you want to redirect only a specific folder or sub folder on your website, you can use the following commands. Replace and folder as per your requirements.

RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} folder
RewriteRule ^(.*)$$1 [R,L]


Remember :

1. After you modify your .htaccess file, remember to restart your Apache server for the changes to take effect.You can do that with the command

$ sudo service apache2 restart

2. If you already have existing code in your .htaccess file, then paste the following commands above them.

3. Replace with your actual domain name.



There are various use cases to redirect HTTP to HTTPs using .htaccess file. Depending on your requirement, use the appropriate piece of code mentioned above. Don’t forget to restart your apache server and clear your browser cache after you modify your .htaccess file.

About Sreeram Sreenivasan

Sreeram Sreenivasan is the Founder of Ubiq, a business dashboard & reporting platform for small & medium businesses. Ubiq makes it easy to build business dashboards & reports for your business. Try it for free today!