In a url the string that follows after http:// or https:// is known as domain name. When we type the domain name of a website, it opens the default home page of the website. The homepage is the default page having file name index.html or index.php depending upon our requirement. That is, when we type domain name of the website, the web server automatically serves the index.html or index.php file.
It doesn’t matter we type example.com or example.com/index.html, the webserver serves the same file that is the file whose name start with index.* (where * can be html, php or asp etc.)
This is absolutely fine for the website. But it creates some problem to search engines. So, why should we worry about search engines? Because SEO is an important aspect of website development these days. A problem for search engine will seriously impact the SEO.
So, why redirect default index.html to root?
Search engines are web crawlers which continuously crawls the web and indexes whatever new webpage it finds on its way. It maintains a directory onto which it indexes the url of the webpages. If a webpage is accessible via more than one url, the crawlers gets confused which url to index into the directory for found the webpage. Because the search engines can’t index both the url and waste its space. In simple words we should not have any webpage in our website accessed via two different urls. We should have exactly one url for every page of the website. So, this is necessary that default index.html should always redirect to root.
Our target is now to redirect all the url which ends with index.* to / . Say, if someone types example.com/index.html, it should automatically redirect to example.com. This way we always have only one version of url for any webpage and not only home page. If we have a webpage in a directory say example.com/test/index.html, it should also redirect to example.com/test/.
Read more about,
Faqs about canonical and alternate link tags
Meta tags for SEO- A beginners guide
Hreflang to optimize website for various language audiencesMeta tags for SEO- A beginners guide
Recommended read,
To implement this we use server’s htaccess file. This is server’s per directory based configuration file. Settings in this file are applicable to all the files and sub-directories below the directory on which the htaccess lies. Lets see how to implement url redirection in htaccess file.
Redirect index.html to root
Below four lines of code will redirect all index.html and index.php to root that is /.
RewriteEngine On
RewriteCond %{THE_REQUEST} ^.*/index\.html
RewriteRule ^(.*)index.html$ /$1 [R=301,L]
RewriteCond %{THE_REQUEST} ^.*/index\.php
RewriteRule ^(.*)index.php$ /$1 [R=301,L]
If you like this article, share with your friends. It will inspire me to write more.
Published on March 8, 2021