Contents...
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:
- How To Enable .htaccess In Apache
- How To Enable And Disable PHP Error Logging Using .htaccess File
- How To Set PHP Maximum Execution Time In An .htaccess File
- WordPress htaccess Rules For A Directory
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 LookLinux Submit Article. Stay connected to Facebook.
Leave a Comment