The Montana Webmaster Team
Helping You With All Your Web Development Needs

A Plain English Introduction to .htaccess

Introduction and Background
In most of the beginning web development classes I teach, we discuss the basic file structure of a website and how it relates to the navigation (links) on the site. Most small organization websites are on “shared hosting,” which means that their site is on a web server with potentially hundreds of other sites. Each site itself will probably end up with thousands of files, especially graphic files. The rub is that somehow the web server has to magically know which one of those files is the Home page to send when visitors as for the site. Well, there isn’t any magic, actually. Server software was designed so that each site and directory (folder) has a specific file name that indicates the main (home) page. Yes, that’s right, every folder on your website can have it’s own “home page.” All you have to do is name the file correctly. Most servers use index as the home page, although default is another common name.

Why .htaccess is Important
Then things get a bit more complicated. What if you don’t want your website to work that way? The .htaccess file allows you to tell the server that you want it to act somewhat differently on your website. It doesn’t change how the server acts on the other websites hosted on the same server. The main server configuration file does all that. Let’s look at a few examples examples of how an .htaccess file might be used.

Example 1: You have an old existing website that was built some 5 or 10 years ago. You take Phyllis Erck’s WordPress: Web Development for the Right Side of the Brain class at The Lifelong Learning Center and decide to rebuild your site with WordPress. You don’t want to disrupt the existing site while you are building, but you want the WordPress site to be available for your staff to comment on during the process. So, you put the whole WordPress install into it’s own folder and leave the rest of the site intact. (In fact, many of us more detailed developers like our WordPress installs to be in a nice folder anyway because it’s much tidier.) When the WordPress site is finished, you don’t actually have to move it out of the folder, you can add a line to a .htaccess file that will tell the server that the new home page is actually in the WordPress folder.

Example 2: After you move all your content from your old website into your new WordPress (or Drupal) site, you realize that Google still has all the old addresses and is sending people to your old site. So, you don’t want to delete your old site and lose traffic, but you want Google to send people to the new site. You can put lines in the .htaccess file that will ‘redirect’ any requests for the old pages to the new pages. Also, when Google sees those redirects, it will put the new page address in its database.

Example 3: You have a programmed site that allows viewers to see the members of your organization and click on a photo to go to a bio. The addresses to each member’s bio page is really ugly and includes code ?Member=6. (This is called a query string and is created by the programming that runs your site.) You are a smart marketer and  would like that to be changed to their name that is in the website database, instead of the query string. You can put a few lines of code in a .htaccess file that will make this happen. By the way, this is also how “permalinks” works in WordPress!

So, What’s with the Dot?
An htaccess file isn’t something that exists for the viewer to see. It is only a set of instructions for the server to know what to send to the viewer. And, it is typically a “hidden” file, which is why it starts with a period. That means that there could be a .htaccess file on your server, and your FTP program or Dreamweaver or whatever software you are using doesn’t show it to you when you go to look at the files on the server. The fact that this file is hidden could cause you a lot of problems if you decide to start a new one and overwrite an existing .htaccess file that you didn’t know was there. After you download the file, it can also cause you a lot of grief trying to open it because your program might not see it either! But you can open Notepad, or Word or Dreamweaver, go to File -> Open and choose All Files. You should be able to see it then.

How to do a 301 Redirect
A 301 Redirect tells the server that requests for a particular URL should go somewhere else. Let’s say that we have a folder called News with  a lot of files in it. Then you decide to make subcategories for your news pages and you move the files into your new folders and fix the links. The problem is that Google still has links to all the old addresses. You don’t want to put the files in both folders; so, you do a 301 Redirect. Here is a potential example with a page about a Business Fair.
Redirect 301 /News/BusinessFair.html http://www.domainname.com/News/Events/BusinessFair.html

It is very  important that you notice the spaces:

  1. Between Redirect and 301
  2. Between 301 and the first address
  3. Between the first address and the second address

These spaces allow the server to separate and use each part of the code.

Also, note that the second address includes the full http address to the file. The www is not required, unless it is required by your web server setup. In fact, there are some interesting things you can do with the .htaccess file related to the www, but that is beyond the scope of this article.

A .htaccess Example
TheComputerGal.com has a page for recommended reading to accompany my various courses, along with a few other topics of interest. Rather, it used to have several pages of recommended reading because I had to make a separate page for the books for each topic. Then I put the book list in a database and programmed it to pick up whatever topic of books I want for each link from the various courses.

The problem is that now I have old pages and new pages on the site. I don’t want to delete the old files because Google is still sending people there – and, there might be a random link from my own site that I missed. But, a few lines in a .htaccess file will make sure that everything goes to the new page.

Redirect 301 /Information/Articles/RecommendedReadingLayout.html http://www.thecomputergal.com/Information/RecommendedReading.php?Topic=WebDevelopment
Redirect 301 /Information/Articles/RecommendedReadingProgramming.html http://www.thecomputergal.com/Information/RecommendedReading.php?Topic=WebDevelopment
Redirect 301 /Information/Articles/RecommendedReadingWeb.html http://www.thecomputergal.com/Information/RecommendedReading.php?Topic=WebDevelopment
Redirect 301 /Information/Articles/RecommendedReadingWebMarketing.html http://www.thecomputergal.com/Information/RecommendedReading.php?Topic=WebDevelopment

Notice that the old pages were

  1. RecommendedReadingLayout.html – books about website layout and design
  2. RecommendedReadingProgramming.html – books about programming
  3. RecommendedReadingWeb.html – books about any web related topic
  4. RecommendedReadingWebMarketing.html – books about web marketing

Notice that they all now go to Information/RecommendedReading.php?Topic=WebDevelopment

You can try this. Go to http://thecomputergal.com/Information/Articles/RecommendedReadingLayout.html. Notice that the address jumps to http://www.thecomputergal.com/Information/RecommendedReading.php?Topic=WebDevelopment. That is the redirect working!

Being More Efficient with your Redirects
If you convert your site to .php and want all the former .html files to now end in .php. You don’t want to have to make a new 301 Redirect line for every single page on your website; so, you do some coding that tells the server to go to a .php file for every .html file.

About The Author


Nora McDougall-Collins started her online career in the newspaper industry, working with the staff of small community newspapers to put their content online. Since then she has been training small business owners and staff about marketing their businesses online and how to create and maintain websites.

Comments are closed.