1. Home
  2. Knowledge Base
  3. Hosting Management
  4. cPanel
  5. How to Redirect www or Non-www URLs With .htaccess via cPanel?

How to Redirect www or Non-www URLs With .htaccess via cPanel?

You can make it mandatory for users to divert traffic from the URL for www to non-www and vice versa with respect to your domain. If you are a cPanel customer, you can include an instruction to redirect users from www URLs to non-www or in your .htaccess file.

To redirect www to non-www URLs by using .htaccess

Step 1: Log into your account on cPanel.

How-to-redirect-www-to-non-www-login-to-cpanel

Step 2: Edit or create an .htaccess file in the document root of your domain with the File Manager.

How to find the .htaccess file?
Step 1: If you can’t find the .htaccess file here go to Settings on the top right of the File Manager


Step 2: In the Preference window that pops up, mark the Show Hidden Files(dotfiles) option and click on Save


Step 3: If you still don’t see the file click on File on the top left of the File Manager

Step 4: Enter .htaccess as the filename in the New File Name textbox.

Step 5: Enter a directory where you want to place the .htaccess file in the below section. The directory section is preconfigured to have “public_html” as the default selection.
Step 6: Now click on Create a New File
How-to-redirect-www-to-non-www-edit-.htaccess-

Step 3: This code should be added to the bottom of your file

# Redirect www URLs to non-www URLs - like http://www.yourdomain.com to http://yourdomain.com 
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.yourdomain\.com [NC]
RewriteRule (.*) http://yourdomain.com/$1 [R=301,L]

Step 4: Click on Save Changes and Close.

How-to-redirect-www-to-non-www-editing-.htacesss

To redirect non-www URLs using .htaccess

Step 1: Log into your account on cPanel.

Step 2: Edit or create an .htaccess file in the document root of your domain using The File Manager.

Step 3: Include this code in the final line of the document:

# Redirect non-www URLs to www URLs - like http://yourdomain.com to http://www.yourdomain.com 
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.yourdomain\.com [NC]
RewriteRule (.*) http://www.yourdomain.com/$1 [R=301,L]

Step 4: Click on Save Changes and Close.

How-to-redirect-non-www-to-www-editing-.htaccess

Here’s an illustration of how the non-www redirect for URLs that are www after setting up the .htaccess file in cPanel.

blogger.com-non-www

Redirects to:

blogger.com-non-www-to-www
Was this article helpful?

Related Articles