Let’s understand how .htaccess redirect non-www to www and index.html to domain name. Before that, we first need to understand, what is .htaccess?
htaccess-redirect-non-www-to-www

What is a .htaccess file?

.htaccess files contains codes. Web Hosting servers uses Apache software to execute these codes. These codes are executed to modify the configuration and to add custom functionalities to Apache Web Servers. Such as redirection, protection etc. In this article, we will discuss .htaccess redirection mechanism to resolve canonical issues of our Web hosting Servers.

What are canonical issues?

Canonical issues arise when several different URLs point to the same page. For example, you have a domain name xyz.com. Now your domain name can open through four different URLs such as:

  1. http://xyz.com
  2. http://www.xyx.com
  3. http://xyz.com/index.html
  4. http://www.xyz.com/index.html

For Google and Yahoo, canonical URLs can become major issues. These search engines get confused and cannot figure out, which is the best URL to be indexed. So to resolve the canonical issues, we can create .htaccess file and upload it to our root directory of our Web Hosting Server (inside the public_html folder). So let’s see how to create it.

How to create a .htaccess file?

We can create this file by writing codes and saving it with file name .htaccess, through any Text Editor like notepad. Remember: .htaccess is a file name extension, not a file name. Here we will discuss step by step method to create this file.

Step 1: Open Notepad

Step 2: Write your codes

Step 3: Save your file with file name “.htaccess” and select all files in the file type.


Let’s understand the redirection codes.

.htaccess redirect non-www to www

The following code will help you to redirect your website traffic from non-www URLs to WWW URLs.

RewriteCond %{HTTP_HOST} !^www.your-domain.com
RewriteRule (.*) http://www.your-domain.com/$1 [R=301,L]

.htaccess redirect index.html to domain name

The following code will help you to redirect your website traffic from index.htm to domain name.

RewriteCond %{THE_REQUEST} ^.*/index.html
RewriteRule ^(.*)index.html$ /$1 [R=301,L]

Redirection of index.php to domain name

The following code will help you to redirect your website traffic from index.htm to Domain Name.

RewriteCond %{THE_REQUEST} ^.*/index.php (We just need to change the extensions)
RewriteRule ^(.*)index.html$ /$1 [R=301,L]
Summary
Article Name
.htaccess redirect non-www to www and index.html to domain name
Description
Beginners guide, understand how to resolve canonical issues, by .htaccess redirect non-www to www and index.html to domain
Author
SHARE
Previous articleWhat is a Domain Name and its Types
Next articleLearn robots.txt disallow and allow commands with example
I am a google certified professional, digital marketing expert and a webmaster, currently motivating peoples for digital marketing and online business/brand promotions. I do provide website development and digital marketing services through my company Webenfolds.

LEAVE A REPLY