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

  1. Step 1: Access cPanel File Manager

    Log in to your cPanel account and navigate to the File Manager.

  2. Step 2: Locate or create the .htaccess file

    Navigate to the document root of your domain (typically public_html). If an .htaccess file doesn't exist, create one. If it exists, open it for editing.

  3. 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.com to http://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.com to http://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.com with your actual domain name in both examples.

  4. Step 4: Save and close the file

    Click "Save Changes" and close the file editor.

  5. 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 .htaccess file, 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:// to https:// in the code examples above.