In this tutorial, you will learn how to redirect your website from HTTP to HTTPS version using .htaccess file. In the world of Cyber Crime, it is necessary to use SSL to protect your website and user data. HTTPS adds a layer of security when transmitting data from a client to a server.
HTTPS stands for Hyper Text Transfer Protocol Secure. HTTPS makes it harder for hackers trying to steal confidential information such as credit card numbers, passwords, etc.
Requirements:
- SSL: You need to buy ssl certificate to install on your web server or hosting.
- FTP Account: You will need an FTP account to edit your .htaccess file if using a VPS or dedicated server without cPanel. For cPanel users, they can go to cPanel and under Files module open File Manager. .htaccess file is in the home directory of your website which looks like /public_html.
Redirecting HTTP to HTTPS in WordPress website
WordPress makes a default .htaccess file when installed.
Default WordPress code for .htaccess file is given below in case you mess the code and your site goes down you can use this code to reinstate your website.
# BEGIN WordPress
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress
.htaccess Code to redirect WordPress website from HTTP to HTTPS in shared hosting servers
Replace the default code with the code given below
Case1: Redirect http://yourdomain.com to https://www.yourdomain.com
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.yourdomain.com/$1 [R=301,L,NE]
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Case2: Redirect http://www.yourdomain.com to https://yourdomain.com
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
.htaccess Code to redirect WordPress website from HTTP to HTTPS in hosting servers when the server is behind a firewall
Case1: Redirect http://yourdomain.com to https://www.yourdomain.com
RewriteEngine on
RewriteCond %{HTTP_HOST} ^yourdomain.com [NC]
RewriteRule ^(.*)$ https://www.yourdomain.com/$1 [L,R=301]
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
# BEGIN WordPress
# The directives (lines) between `BEGIN WordPress` and `END WordPress` are
# dynamically generated, and should only be modified via WordPress filters.
# Any changes to the directives between these markers will be overwritten.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Case2: Redirect http://www.yourdomain.com to https://yourdomain.com
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.yourdomain.com [NC]
RewriteRule ^(.*)$ https://yourdomain.com/$1 [L,R=301]
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
# BEGIN WordPress
# The directives (lines) between `BEGIN WordPress` and `END WordPress` are
# dynamically generated, and should only be modified via WordPress filters.
# Any changes to the directives between these markers will be overwritten.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Redirecting HTTP to HTTPS in HTML website(NON-Wordpress)
Case1: Redirect http://yourdomain.com to https://www.yourdomain.com
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.yourdomain.com/$1 [R,L]
Case2: Redirect http://www.yourdomain.com to https://yourdomain.com
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://yourdomain.com/$1 [R,L]
Leave a Reply