How to Redirect Http to Https Using htaccess File

How to Redirect Http to Https Using htaccess File
.htaccess is short for Hypertext Access, and is a configuration file for the Apache Web Server that is used to override the main server configuration. .htaccess operates at directory level and you can even set different configuration to different directories within your website. Common uses of .htaccess including URLs redirection, error page customization, directory protection with a password, https forcing, hot link prevention, cache control, and etc. In the article below, we’ll show you how to redirect http to https by using a .htaccess file. This method is particularly useful if you’ve installed a SSL certificate on your website and would like to automatically redirect your visitors from http to the https web version.

Creating .htaccess File

.htaccess file can be easily created by using any plain text file editor and it must be placed within the root directory of a website domain with .htaccess file extension. If you're using multiple .htaccess files, do take note that the files higher up in the file path always take precedence.

Redirect Http to Https

To force https redirection, simply add the following code to the .htaccess file and your web server will automatically redirect all pages of your website from http to https.

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

Don’t forget to replace yourdomain.com with your website’s URL. Now try visiting any http pages of your website and you shall redirect to the https version. If you need help in creating the .htaccess file, you may leave your comments in the comment section below.


Frequently Asked Questions and Answers:

1. FTP client does not display .htaccess file?
.htaccess file is invisible by default. Please make sure that you’ve configured your FTP client to display all type of files including the hidden files.

2. I have received a Mixed Content Warning on the https page?
This is probably due to your page contain unsecured resources. It can be image file, jquery libraries, CSS file, video file, and etc. Try removing the unsecured resources and the warning shall go away.


EmoticonEmoticon