Loading

Redirect http to https using .htaccess file

Redirect http to https using .htaccess file

Sometimes it becomes hard to change all existing website urls or links to use https. And as of 2018 September chrome shows insecure warning if website not using SSL or serving pages over http. SSL is freely available with most of top hosting providers. If not one can get free certificate from Startcom SSL or Letsencrypt. In this post we will explain you a way to add small rule to redirect http to https using .htaccess file. 

.htaccess File

To find or locate your htaccess file login to file manager using FTP or cPanel/Plesk File manager (if you are using any one of them). From root of your domain/website usually public_html in cpanel, httpdocs in plesk. For others it varies, on ubuntu or linux/unix operating system you can find the .htaccess file under /var/www/html).

Note:

If you are using cPanel, make sure to check show hidden files(dotfiles) using gear icon Settings link near top right of cPanel file manager.

On linux/unix apache2 domain root, to view dot files (which are usually treated as system files) press Ctrl+H

Rules to Redirect http requests to https

Once you locate the .htaccess file, edit it using your favourite editor. Add the following code to your htaccess file

RewriteEngine On #do not add this line if its already present in your htaccess file
RewriteCond %{HTTPS} !on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI) [L,R=301]

thats all once you added the above code to your htaccess file it starts sending all non https requests to https.

Rules to Redirect Particular Domain to https

Use the following code to redirect particular domain to https

RewriteEngine On #do not add this line if its already present in your htaccess file
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(www\.)yourdomain\.tld [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI) [L,R=301]

Do not forget to change the domain (yourdomain\.tld to your domain name which you want to redirect to https)

If you are looking for more set of .htaccess rules add comment.