A Complete Guide to .htaccess for SEO

Last Updated on

Do you want to change the default index page of a directory?

Do you want to protect your password for directories?

Linkbuilding is the most popular topic in the SEO community no doubt. But, often times people forget that technical SEO lays the foundation of linkbuilding and SEO. All of your efforts will be rendered wasted if you’re not armed with information architecture, solid code and redirects in place.

Many times, we need to make configuration changes on a per-directory basis. That’s when the .htaccess file comes in. Today, I want to talk about this particular file. One of the basic examples of .htaccess is htaccess redirect.

There are many ways you can implement 301 redirects. There are bad redirects as well. 302 redirects signify bad news. However, the file only works in Apache Web server on both Windows, Unix/Linux operating systems.

Let’s find out what the file actually is and how you an SEO can reap benefits from it:

What Is .htaccess file?

.htaccess is a file that stands for Hypertext Access. It comes with a set of rules for the directory that it is contained within. Moreover, it creates a set of rules for all sub-directories as well. The limitation you have to face with this file is that you will need an Apache based web server to create this file.

Note: if you aren’t hosting on your own server, you must consult with your hosting provider. Ther are hosting providers like Bluehost that will help you out with redirects.

Let’s expand each of these topics in depth:

Why should you learn how to create the .htaccess file?

An SEO expert should know how to create the .htaccess file. When working with SEO procedures & strategy making, there will be clients who will require you to work with their directory, protecting directories with password.

.htaccess is not a file extension. It is the filename in full. Someone may at first think the file could actually be: file.htaccess. No, it is not! It is simply called .htaccess. The file will be functional once it is embedded in all files, and all subdirectories within a particular directory.

So, at some point, you’ll have to work with this certain file.

Here’s what else you can do with the .htaccess file:

  • Redirect visitors from one directory or one page to another
  • Protect directories by password
  • Transform the default index page of a directory
  • Shield hot-linking of images from your website
  • Forcing a specific version of your website to your visitors.
  • Redirecting your visitors from one website to another.

Working With .htaccess File (Some Codes)

You will have to do a lot of editing and creating with the .htaccess file. So, you’d better have a text editor to do the editing. Normally a text editor, such as Notepad will suffice. Moreover, a free copy of PSPad will do well for easy editing.

But what are the codes you can use?

Lets see –

Non WWW to WWW Version (Force WWW on your domain)

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

 

WWW to Non-WWW (Force non www version for your domain)

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

Redirect certain extension (If you change your URL extension from .php to .html)

RewriteEngine On
RewriteCond %{REQUEST_URI} .php$
RewriteRule ^(.*).php$ /$1.htm [R=301,L]

Redirect old domain to a new domain

RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(.*)$ http://example.net/$1 [L,R=301,NC]

Redirect Old File/Page To New File/Page (Single File)

Redirect 301 /oldfile.htm http://example.net/newfile.htm

If you wish to look at files in your FTP software, you must allow such permission in your FTP client to view the hidden files on the remote server.

When you’re finished with editing, make sure you save the file with double quotes in Windows. Use double quotes to save the file. For example: save file as “htaccess”.

Another example of what you could include in a .htaccess file:

AuthName “Member’s Area Name”

AuthUserFile/path/to password/

File/.htpasswd

AuthType Basic

Require valid-user

ErrorDocument 401 /error_pages/401.html

AddHandler server-parsed.html

The above example described show how you can implement password protection on the directory. With that .htaccess file, you can offer redirection to a custom error page if a user couldn’t login properly.

Once you are done with creating a .htaccess file, which should look similar to the example discussed above, you need to upload the file. You need to use a file transfer protocol (FTP) program to upload the file.

Note: when uploading the .htaccess file, it is important that you upload the file in ASCII mode. ASCII is a method of data transferring, which is different from Binary. Do not transfer the file through Binary mode. A FTP program could default to Binary, so make sure you change the transfer mode.

Where Should You Put the .htaccess File?

You have to insert the .htaccess file in the root folder of your domain. The root folder of your domain is where the robots.txt file and other contents/files of your website is located.

Summary

You can make ample configuration changes on a per-directory basis with a .htaccess file. Understand everything about this file from this article.

Leave a Comment

5 Shares
Tweet
Share
Pin
Share