Knowledgebase

How to Redirect the Domain from HTTP to HTTPS with www

Print
https
www
http
151

To redirect all website requests to the domain with www prefix and force https usage at all times, follow these steps:

Step One: Access .htaccess File

  1. Log into cPanel
  2. Open File Manager from the Files section
  3. Navigate to your website's main folder public_html
  4. Look for the .htaccess file or create it if it doesn't exist
Note: If you can't find the .htaccess file, make sure to enable "Show Hidden Files" in File Manager.

Step Two: Choose Redirect Type

You can choose one of the following solutions based on your needs:

Solution One: Force WWW Only

To redirect all visitors from domain.com to www.domain.com:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$ [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [L,R=301]

Solution Two: Force HTTPS Only

To redirect all visitors from http to https:

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

Solution Three: Force Both WWW and HTTPS (Recommended)

For best results, use this comprehensive solution:

RewriteEngine On

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

# Force WWW
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$ [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [L,R=301]
Important Warning: Make sure you have a valid SSL certificate on your website before applying HTTPS redirects, otherwise visitors will get security warnings.

Solution Four: Optimal and Most Accurate Solution

This solution handles all cases correctly:

RewriteEngine On

# Redirect to HTTPS and WWW
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^(.*)$ https://www.%1/$1 [L,R=301]

Step Three: Save Changes

  1. Copy the appropriate code and paste it at the beginning of the .htaccess file
  2. Save the changes
  3. Test the website to ensure redirects work correctly

Testing Redirects

To ensure redirects work properly, test these links:

  • http://domain.com → should redirect to https://www.domain.com
  • https://domain.com → should redirect to https://www.domain.com
  • http://www.domain.com → should redirect to https://www.domain.com
  • https://www.domain.com → should remain as is

Important Tips

  • Backup: Create a backup of the .htaccess file before editing
  • Order Matters: Place redirect rules at the beginning of the file
  • 301 Code: Tells search engines the change is permanent
  • Comprehensive Testing: Test all website pages after implementation
  • Performance Monitoring: Monitor website statistics to ensure no issues

Troubleshooting

If you encounter problems:

  • 500 Error: Check the code syntax in the .htaccess file
  • Infinite Redirect: Make sure there are no conflicting rules
  • HTTPS Not Working: Verify SSL certificate installation
  • Cache Issues: Clear browser cache and test from a different browser
For Beginners: If you're unsure about the code, start with Solution Three (recommended) as it's the most common and stable.
Was this answer helpful?