in , , ,

Url htaccess Rewrite: Solve Canonicalization Using .htaccess File

Canonicalization is one of the most complicated and confusing query among the webmasters. Canonicalization is the issue which arises due to the presence of the same content on WWW and non-WWW version of the website. At the current time the Search Engine crawlers are not advanced enough to distinguish between two versions of a website URL and hence treat both URLs as different identity with the same content. This situation leads to the problem of ‘Duplicate Content’ and we all know what Google says about it! There are many ways to solve canonical issues and I had discussed the  most notable ones in the below section.

Let’s take an example; suppose my website’s Home page can be opened by different URLs like

http://www.myexample.com/ (www version)

http://myexample.com/ (non www version)

http://myexample.com/index.html

or

http://www.myexample.com/home.php
or

http://myexample.com/index.php
Or

http://www.myexample.com/default.aspx etc.

In this situation; the all URLs will directs to the home page of the website but Google and many more other search engines will treat it as different web pages having same URL and hence can penalize the website. If you too are encountering the situation where even after adopting every white hat SEO technique, your website is showing poor rank in SERP; Canonicalization may be the issue. To bundle up all the complications; you have to fix canonical issue.

Google’s official speaker Matt Cutt’s view about canonicalization

The solution to deal with this serious issue is to designate 301 URL redirection with .htaccess file. The .htaccess is actually a configuration file compatible with web servers running the Apache Web Server software. This file is read by the Apache web server software and can be used for many purposes.

301 redirection with .htaccess file

  • Open a new notepad document and save the file with the name of .htaccess.
  • Now paste the below provided code in the file. Replace DOMAINNAME with your website domain and index.html need to replace by your home page file name. After configuring the code; upload this file on your server using FTTP software.
# For security reasons, Option followsymlinks cannot be overridden.

#Options +FollowSymLinks

Options +SymLinksIfOwnerMatch

RewriteEngine on

RewriteCond %{HTTP_HOST} ^domainname\.com$ [NC]

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

RewriteCond %{THE_REQUEST} ^.*/index.html

RewriteRule ^(.*)index.html$ http://www.domainname.com/$1 [R=301,L]

Redirect Using cPanel

For the users whose web hosting service provides cPanel can redirect the pages without using any code. To redirect using cPanel; Login to cPanel => Go to Redirects and follow the guidelines of the snapshot.

Redirect WWW to non-WWW

WWW to non WWW cpanel

Redirect non-WWW to WWW

non WWW to WWW cPanel

Redirect Using IIS7

If you want to redirect using IIS7 then add the below provided code as the first rule in the “system.webserver” section of the “web.config”.

Redirect WWW to non-WWW:

<rewrite>
   <rules>
      <rule name="www to non www"" enabled="true">
         <match url="(.*)" />
         <conditions>
            <add input="{HTTP_HOST}" negate="true" pattern="^www\.yourdomain\.com$"  />
         </conditions>
         <action type="Redirect" url=http://www\.yourdomain\.com/{R:1}” redirectType="Permanent" />
      </rule>
   </rules>
</rewrite>

Redirect non-WWW to WWW

<rewrite>
   <rules>
      <rule name="non www to www" enabled="true">
         <match url="(.*)" />
         <conditions>
            <add input="{HTTP_HOST}" negate="true" pattern="^www\.youdomain\.com$" />
         </conditions>
         <action type="Redirect" url="http://www\.yourdomain.\com/{R:0}" redirectType="Permanent" />
      </rule>
   </rules>
</rewrite>

Redirect Using nginx

Nginx is one of the most popular servers at the present time. For nginx user the code for redirecting the URL is provided in the below section. Add this code at the top of your website Configuration file.

Redirect WWW to non-WWW

server {
listen 80;
server_name www.yourdomain.com;
rewrite ^/(.*) http://yourdomain.com/$1 permanent;
}
Redirect non-WWW to WWW
server {
listen 80;
server_name yourdomain.com;
rewrite ^/(.*) http://www.yourdomain.com/$1 permanent;
}

Conclusion

I hope that you would have successfully redirected your website URLs to solve canonicalization issue. Finding any query? Share that with me in the comment section below. I will like to hear your feedback about my post.

Keep Tracking Webgranth: Keep Loving Web Technology

What do you think?

Written by webgranth

Comments

Leave a Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Loading…

0

Comments

0 comments

Move Server to New Domain

Move Server to New Domain: How to Transfer WordPress to New Server

WordPress ads manager: List of Best WP ad management Plugins