Apache

Solve The Canonical Website Address With Htaccess

Canonical-Website-Address
mm
Written by Santosh Prasad

What is .htaccess?

.htaccess is a Apache configuration file which is used on running Apache web server software. If we create .htaccess file in web root directory then this file is detected and executed by the Apache web server. It is used to alter the Apache configuration. It is used to enable/disable additional functionality and features. This facilities include basic redirect functionality, for example if a 404 found on the site. This is also used to secure content using password protected or image hot linking prevention etc.

In this article I will explain how to solve the canonical website address with htaccess.

Recently I resolved one issue with htaccess was the canonical website address. Before it was not much an issue, as you can set the canonical address using Google Webmaster tool, which I had done, but that only works for Google, and not to the another search engines like Yahoo and Bing.

Solving The Canonical Website Address With HTACCESS

There are lots of different ways to solve this I found on internet. I tried several bits of code and each time uploaded it into my server via FTP and then tested it and see if it is working. Finally I found the below code that worked for me:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.co.uk$ [NC]
RewriteRule ^(.*)$ http://www.example.co.uk/$1 [R=301,L]
RewriteCond %{THE_REQUEST} ^.*/index.html
RewriteRule ^(.*)index.html$ http://www.example.co.uk/$1 [R=301,L] 

If you want to redirect your visitor to www the above code will work. Above code also redirect the index.html pages
to the correct canonical address, but if you want to redirect to your naked website address then you can put this:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.example\.co.uk$ [NC]
RewriteRule ^(.*)$ http://example.co.uk/$1 [R=301,L]
RewriteCond %{THE_REQUEST} ^.*/index.html
RewriteRule ^(.*)index.html$ http://example.co.uk/$1 [R=301,L] 

Suggested Read:

Thank you! for visiting Look Linux.

If you find this tutorial helpful please share with your friends to keep it alive. For more helpful topic browse my website www.looklinux.com. To become an author at Look Linux Submit Article. Stay connected to Facebook.

About the author

mm

Santosh Prasad

Hi! I'm Santosh and I'm here to post some cool article for you. If you have any query and suggestion please comment in comment section.

Leave a Comment