This article explains how to redirect your website from HTTP to HTTPS to secure user connections, protect sensitive information, and improve SEO rankings. Using HTTPS ensures encrypted communication between users’ browsers and your server.
Why HTTPS Is Important?
- Protects sensitive data like login credentials and payment info.
- Improves user trust and credibility.
- Helps your website rank better in search engines (Google favours HTTPS).
Note: Ensure your server has a valid SSL certificate installed before attempting any redirects.
Prerequisites
- Valid SSL certificate installed on your domain.
- Access to server files via FTP, cPanel, or SSH.
- Basic understanding of .htaccess, PHP, or HTML editing.
3 Ways to Redirect HTTP to HTTPS
Method 1 – Using .htaccess (Recommended for Apache Servers)
- Open your site’s .htaccess file in the root directory
- Add the following code:
RewriteEngine On RewriteCond %{HTTPS} off RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
How It Works:
- RewriteEngine On > enables Apache mod_rewrite
- RewriteCond %{HTTPS} off > checks if the connection is not HTTPS
- RewriteRule (.*) > redirects all HTTP URLs to the HTTPS version
Tip: Use [L, R=301] to make it a permanent redirect for SEO.
Method 2 – Using PHP Function
- Add this PHP function to your site (e.g., in your theme’s header or a common include file):
<?php function redirectToHttps() { if ($_SERVER['HTTPS'] != "on") { $redirect = "https://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; header("Location: $redirect"); exit(); } } redirectToHttps(); ?>
How It Works:
If $_SERVER[‘HTTPS’] is not set to “on”, PHP automatically redirects the browser to the HTTPS version of the website.
Note: Ensure SSL is installed and active before using this method.
Method 3 – HTML Meta Tag (Not Recommended)
- Add this in the <head> section of your HTML page:
<meta http-equiv="Refresh" content="0;URL=https://www.yourdomainname.com" />
How It Works:
- Forces the browser to refresh and load the HTTPS version.
- Less reliable and slower than server-level redirects.
Note: Use only if you cannot access .htaccess or PHP files.
Verification
- Open your website in a browser using HTTP.
- Ensure it automatically redirects to HTTPS.
- Check for a secure padlock in the browser address bar.
This way, you can redirect your website from HTTP to HTTPS. If you need assistance, feel free to contact our support team.
How to Set Up Redirects in cPanel