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

As a cPanel user, you have the capability to compel users to redirect from the www to non-www URL, or vice versa, for your domain. Simply add a directive in the .htaccess file to implement the desired redirection.

Redirecting www to Non-www URLs using .htaccess:

  1. Access your cPanel account.

  2. Create or edit the .htaccess file in the document root of your domain through File Manager.

  3. Add the following code to the file's conclusion:

# 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]

  1. After selecting "Save Changes," close the file.

https://www.milesweb.in/hosting-faqs/wp-content/uploads/2021/10/cp_redirect_www-non_www_htaccess.jpg

Redirecting Non-www to www URLs using .htaccess:

  1. Access your cPanel account.

  2. Create or edit the .htaccess file in the document root of your domain through File Manager.

  3. Add the code that follows to the end of the file:

# 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]

  1. Save your changes and close the file.

https://www.milesweb.in/hosting-faqs/wp-content/uploads/2021/10/cp_redirect_non_www-www_htaccess.jpg

Note: If there is an existing redirection directive in the .htaccess file, you must replace it to modify the redirection target.

 


Was this article helpful?

mood_bad Dislike 0
mood Like 0
visibility Views: 6586