This guide shows you how to configure URL redirects between www and non-www versions of your domain using the .htaccess file in cPanel. This ensures consistent URLs for SEO and user experience by forcing all traffic to use either the www or non-www version of your domain.
How to do it
-
Step 1: Access cPanel File Manager
Log in to your cPanel account and navigate to the File Manager.
-
Step 2: Locate or create the .htaccess file
Navigate to the document root of your domain (typically
public_html). If an.htaccessfile doesn't exist, create one. If it exists, open it for editing. -
Step 3: Add the appropriate redirect code
Choose one of the following options based on your needs:
Option A: Redirect www to non-www URLs
Add this code to redirect
http://www.yourdomain.comtohttp://yourdomain.com:# Redirect www URLs to non-www URLs RewriteEngine on RewriteCond %{HTTP_HOST} ^www\.yourdomain\.com [NC] RewriteRule (.*) http://yourdomain.com/$1 [R=301,L]Option B: Redirect non-www to www URLs
Add this code to redirect
http://yourdomain.comtohttp://www.yourdomain.com:# Redirect non-www URLs to www URLs RewriteEngine on RewriteCond %{HTTP_HOST} !^www\.yourdomain\.com [NC] RewriteRule (.*) http://www.yourdomain.com/$1 [R=301,L]Replace
yourdomain.comwith your actual domain name in both examples. -
Step 4: Save and close the file
Click "Save Changes" and close the file editor.
-
Step 5: Test the redirect
Visit both the www and non-www versions of your domain to verify that the redirect works correctly.
Common issues
- Existing redirect rules: If there are already redirection directives in your
.htaccessfile, you may need to replace or modify them to avoid conflicts. - HTTPS considerations: If your site uses HTTPS, make sure to update the redirect URLs from
http://tohttps://in the code examples above.