How to force your website url with www or non-www using htaccess

Choosing a preferred domain format is a recommendation from Google and if you will succees in the business of the internet, you must take what Google says very seriously. In this post we examine the issue of preferred domains- whether to allow your url use the www or not to use the www. This has been referred to url canonicalization.

You will also want to read:

How to Create a MySQL Database, a User and Delete Database in Cpanel
How to Create a SubDomain in Cpanel
How to Create a Website/Domain Redirect in cPanel
How to Backup My Website in cPanel
How to optimize your website to use less server resources

But What is URL Canonicalization?

Canonical URL is an important term that every website owner should be aware of. Even if you have heard about it or not, it affects your website performance and you should know what role Canonical URL plays in the optimization of a website. It is relevant to Search Engine Optimizzation and while doing SEO, we need to check both On Page SEO and Off Page SEO. But if you are not doing your On Page SEO properly, you will not get the good results, no matter if you are performing Off Page SEO well. So we need to delve take URL Canonicalization (Canonical URL) which is a part of On Page SEO very seriously.

To define canonical URL we can say it is an authoritatively correct URL for a resource. Canonicalization is the process of choosing the best URL when there are several options. Let us try to explain this further. Let's say, the following is the URL to access the homepage of Todhost website

www.todhost.com
todhost.com

The above is a canonical URL example. Although these are URLs pointing to the home page, but technically speaking, all of these URLs are different. A web server sees these unique URLs as two unique pages. Thus, a web server can return completely different content for all the URLs mentioned above. Since different content doesn’t exist on each of these variation URLs, and if URL Canonicalization is not done, Search engines view it as a duplicate content. To make sure the search engines are indexing the correct page, you need to select which variation of the URL you want to set as a canonical URL or canonicalized URL.

Once you have picked any version of URL, say canonical URL, stick to it. A canonical URL is the one that you want visitors to see.

Canonical URL - Canonicalization


How Do You Implement Canonicalization?

We will now look at how to create canonical URL. This is a very small task. We just need to use a canonical tag to apply Canonicalization. Your website might have been returning same content in both www and non-www versions. In this case, you can apply the canonical tags (rel=”canonical”) to indicate that the second URL is a Canonical URL of the first one. We look further into that below.

With most content management systems, you can find url re-writing softwares which makes room for you to choose whether to use the url with or without www. This is called canonicalization.

However, there is a simple way of doing this using the .htaccess file.

Many customers at some point request to force either the www or non-www version of their site to display in their visitor's browser. For example always have www.todhost.com or simply todhost.com display in their visitor's web browser. There is a common thought that forcing one format is better for search engine optimization. This article will guide you through how to perform this action on your .htaccess in the cPanel for your primary domain.

How to force www or non-www in your .htaccess file in cPanel

Log into your cPanel.
Find the Files category and click on the File Manager icon.

A popup box will appear. For the primary domain, click on the Web Root radio button. For addon domains, click on the dropdown and find your desired addon domain name. Be sure the checkbox next to Show Hidden Files is checked. Click the Go button to enter the File Manager.

You should now be in the root folder of the domain you chose. Look for the .htaccess file and right click on it. This brings up a menu. Find and click on the Edit option. If you get a popup box, simply find and click the Edit button in the lower right corner to continue to the editor.


Also read:

How to Reset or Change to a New CPanel Password
How to Set Up and Delete a Cron Job
How to Setup Google Apps MX, CNAME and SPF records
How to upload files to a website
Overview of Quickinstall


You are now in the text editor. Place either selection of code in the file and click on the Save Changes button at the top right corner of the screen. Be sure to replace 'example.com' with your actual domain name. NOTE: do not place both selections of code in the file as it will cause an error.
#Force www:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301,NC]

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


Also read:

PHP Configuration in cPanel
Step by Step Approach to Password Protect a Directory in cPanel
The htaccess file and its uses



Now, when you type in your domain name with either www in front or not, it should display as you have set it in the .htaccess file.

You now know how to canonicalize your url to use www or not to use the www.

 

Why Should You Care About Canonical URL or URL Canonicalization?

 

The problem is with search engine bots. They cannot decide which version of the URL they should add in their index. If you have two or more pages resulting in the same content, the bots will assume one is a duplicate copy of the other. The worst part is, it can even get your website penalized. To avoid this, you can use rel=”canonical” tags to let the search engines know that which is the original and which one is a copy of it. This can save you from duplicate content penalties.

Parameter Handling For Dynamic Parameters

You may need to tell search engines about any parameters you would like ignored. Ignoring certain parameters can reduce the duplicate content in Google’s index which makes your site more crawlable. For example, if you specify that the parameter category or sessionid should be ignored. Google will consider https://www.abc.com/new-cloths to be the same as https://www.abc.com/Men/clothing.php.

 

When this happens, site owners suffer rankings and traffic losses and engines suffer lowered relevancy. So to avoid this, we need to use Canonical URL tag.

Best practices in URL Canolnicalization

You can avoid the negative conseuences of bad canonicalization or, at least minimize them, by engaging in these best practices:

  • Use 301 redirection to ensure that your home page is only found at one URL.
  • Link consistently to your home page from within your own site. Use a single URL for your home page. Don’t mix in instances of ‘www.yourdomain.com/index.html’ with ‘www.yourdomain.com’. If you aren’t doing this properly right now, a quick change may have a big impact on SEO.
  • Don’t use tracking IDs in internal site navigation. A lot of sites add stuff like ‘?source=blog’ in their navigation. That lets them use their analytics reports to track user movement within, to and from their site. Instead, use your web analytics referrer and navigation path reports. If you must use tracking IDs, change your software to use a hash mark (a ‘#’ sign) instead of a question mark. Search engines ignore everything after the hash, so you’ll avoid confusion.
  • Don’t use tracking IDs in organic links from other sites. If you get a link on another site, and want it to help with your SEO, don’t put a tracking ID in that, either.
  • Be careful with pagination. Many sites have pagination, where visitors can click a 1, 2, 3 etc. to jump to later pages in search results, product lists or articles. That’s fine, but make sure that each page has a single URL with unique url, title and description.
  • Set up preventative redirects. Make sure that ‘ourdomain.com’ 301 redirects to ‘www.yourdomain.com’.
  • Exclude ‘e-mail a friend’ pages. Most content management systems that have ‘e-mail a friend’ options direct the user to a unique page that has the same form and content. But every instance of that page has a unique URL like ‘ID=123’, to tell the server which product or article to forward.
  • Use common sense when building your site.

Final Words

URL canonicalization is an important because:

  1. If there are multiple versions of a page with the same content, Google and other search engines won’t be able to identify which version to include or exclude from the search results
  2. If the same content can be found across multiple URL’s, search engines are unable to identify which source was the original
  3. With multiple URL’s containing the same content, search engines would not know where to direct link metrics such as trust and link equity to if users are linking to multiple versions of the same page


However, you need to carefully implement URL canonicalization and avoid mistakes. Google recommends these best practices:

  • A large portion of the duplicate page’s content should be present on the canonical version.
One test is to imagine you don’t understand the language of the content—if you placed the duplicate side-by-side with the canonical, does a very large percentage of the words of the duplicate page appear on the canonical page? If you need to speak the language to understand that the pages are similar; for example, if they’re only topically similar but not extremely close in exact words, the canonical designation might be disregarded by search engines.
  • Double-check that your rel=canonical target exists (it’s not an error or “soft 404”)
  • Verify the rel=canonical target doesn’t contain a noindex robots meta tag
  • Make sure you’d prefer the rel=canonical URL to be displayed in search results (rather than the duplicate URL)
  • Include the rel=canonical link in either the <head> of the page or the HTTP header
  • Specify no more than one rel=canonical for a page. When more than one is specified, all rel=canonicals will be ignored.
  • 54 Users Found This Useful
Was this answer helpful?

Related Articles

I have a full backup of account through cPanel. How do I restore it?

It is recommended that you maintain healthy backup of your website and possibly download the...

How To Create, Edit, and Delete a File in cPanel Using File Manager

This tutorial will be useful for you to understand how to create a file, edit a file or delete...

How to Create and Manage a MySQL Database

Create MySQL Database and User via cPanel The MySQL Database Wizard is another useful tool...

How do I create and remove an Addon Domain?

An add-on domain is a separate domain name that you add to your web hosting subscription with...

How to Set Up and Delete a Cron Job

A cron job allows you to run a certain command at times set by the job. For example, you could...