htaccess code to redirect a domain to https

The codes below can be used to redirect your visitors from http (normal/insecure) to https (encrypted/secure). These codes are to be added in the htaccess files present in root directory for the domain. If you’re new to the term htaccess, you can read more about it in the post Introduction to .htaccess file.

You only have to use ANY ONE of the following code. Use CODE 3 if you’re trying to redirect only one domain ( usually the primary domain ) associated with the cPanel which has multiple addon domains. This will ensure other domains will work normally while only the domain that’s set to redirect will redirect to the secure version.

CODE 1:

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]]

CODE 2:

RewriteEngine On
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]

CODE 3:
Make sure you change yourdomain.name with your domain. For exmaple ^yourdomain\.name.* would be ^errorlog\.in.*

RewriteEngine On
# Redirect to https, but only apply to one domain, not to add on domains
RewriteCond %{SERVER_PORT} !=443
RewriteCond %{HTTP_HOST} ^yourdomain\.name.* [OR]
RewriteCond %{HTTP_HOST} ^www\.yourdomain\.name.*
#In the next line you can either use the domain with or without www.
RewriteRule ^ https://www.yourdomain.name%{REQUEST_URI} [R=301,L]

If you’re trying to redirect a WordPress website to its secure version, make sure you change the site and home url’s (Dashboard > Settings > General Settings) to https before making the above changes

/

Report (Please include link to post while reporting)

Leave a Reply

Your email address will not be published. Required fields are marked *