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
- Log into cPanel
- Open File Manager from the Files section
- Navigate to your website's main folder
public_html - Look for the
.htaccessfile or create it if it doesn't exist
.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]
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
- Copy the appropriate code and paste it at the beginning of the
.htaccessfile - Save the changes
- Test the website to ensure redirects work correctly
Testing Redirects
To ensure redirects work properly, test these links:
http://domain.com→ should redirect tohttps://www.domain.comhttps://domain.com→ should redirect tohttps://www.domain.comhttp://www.domain.com→ should redirect tohttps://www.domain.comhttps://www.domain.com→ should remain as is
Important Tips
- Backup: Create a backup of the
.htaccessfile 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
.htaccessfile - 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