Cloud Service >> Knowledgebase >> cPanel >> How To Redirect www Or Non www Url With .Htaccess File Via cPanel?
submit query

Cut Hosting Costs! Submit Query Today!

How To Redirect www Or Non www Url With .Htaccess File Via cPanel?

Redirecting www from non-www URLs and vice versa would be considered a very crucial function of website management because this enforces the same experience to users as well as search engine optimization. All this can now be efficiently done through an .htaccess file that is easily accessible through the web hosting control panel which is popularly known as cPanel.

 

Go ahead and log in to the cPanel. Where the .htaccess file exists varies with different web hosting services, but most are located in the public_html directory. If you cannot see a file listed, maybe it's a hidden one. To view the hidden files, find the "Show Hidden Files" option in your cPanel file manager.

 

If you don't already have an .htaccess file, you will need to create one. You can open a new file called ".htaccess" in any plain text editor, complete with the dot and quotes, and upload it in your public_html directory. If it already exists you may download the file, make your requested alterations, then reupload.

There are two scenarios here for redirects:

1. Redirect www to non-www:

To redirect www.yourdomain.com to yourdomain.com, add the following code to your .htaccess file:

 

RewriteEngine On

RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]

RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

2. Redirecting non-www to www: 

To redirect yourdomain.com to www.yourdomain.com, use this code instead:

RewriteEngine On

RewriteCond %{HTTP_HOST} !^www\. [NC]

RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

 

Let's break down the code snippets:

RewriteEngine On: This turns the rewrite engine ON, so you may manipulate the URL.

RewriteCond: This is under what conditions you will tell the server to execute the rewrite.

RewriteRule: This states how the URL should be rewritten.

R=301: This flag specifies that this is a permanent redirect, which is beneficial for search engine optimization purposes.

L: The flag means that the server should stop processing rules if this is the matched one.

Of course, don't forget that these redirects use HTTP. If your site uses HTTPS-and you should do, given the importance of using Secure Sockets Layer/Transport Layer Security -you'll want to replace "http://" with "https://" in each of the RewriteRules.

 

Next step: Save the file and upload it back to your server. Clear browser cache and test your redirects.

 

Note: If you are using WordPress, you also have to add one more rule, since WordPress uses internal URL handling you need to avoid conflicts:

 

RewriteCond %{REQUEST_URI} !^/wp-admin

RewriteCond %{REQUEST_URI} !^/wp-login.php

 

This piece of code needs to be placed directly after RewriteEngine On and just before the other lines that start with RewriteCond.

 

Care must be taken when handling the .htaccess file, as this file must not be modified directly since it will give a server error, but the original backup should be given before effecting any change. Therefore, in case something is wrong after you have altered your .htaccess file, revert it to the backup right away.

 

It is, as well, good practice to update any hardcoded internal links in your website after adding the redirects to use only one preferred URL format, either www or non-www, so you won't have to leave many unnecessary redirects and maintain consistency on your site.

 

Remember that a change in the URL structure can impact your search engine optimization. Although 301 redirects alleviate many problems, you want to pick one format and stick with it long term. In addition, you want to make sure to let the search engines know your preferred domain by utilizing Google Search Console and Bing Webmaster Tools.

 

If you don't feel like messing directly with the .htaccess file, you can use URL redirection tools located within cPanel that most of your hosting providers include. These tools are intuitive and easy to set up without having to make any file changes directly. However, using the .htaccess method offers much more flexibility and freedom of control for you.

 

If you're using a content management system like WordPress, Joomla, or Drupal, you would use a wordpress plugin or extension to manage your URLs. These do tend to clutter your site with extra overhead for the sake of functionality, though. Usually, for a redirect it's going to be better to use a .htaccess method, which handles changes to redirects at the server level.

Concluding Up!

To redirect www to non-www URLs - or vice versa - can be done simply via a .htaccess file that is used by using cPanel. This makes all the difference in the world for the user experience on your site and your SEO. You will know that you're maintaining that consistent, professional online presence by following these steps and noting the implications of making any such changes in URL structure.

Cut Hosting Costs! Submit Query Today!

Grow With Us

Let’s talk about the future, and make it happen!