Beginners Guide to The .htaccess File

apche-web-serverThe .htaccess file is a very powerful text control file found on Apache servers, (note: even if they run on windows) that allows control over various components of the web server.

For example the .htaccess file can be used to:

  • Password protect pages/directories
  • Block users
  • Do redirects
  • Change default home page
  • Add file extensions
  • etc

What exactly you can do with the file is determined by the server administrator, and so just because it is possible to use the .htaccess file to configure, say password protection, it doesn’t mean that you will be able to do it on your hosting package.

Name, Structure and Flow

The file name is itself very confusing as there isn’t really a name just a file extension.

Most files take the form name.extension.

However the .htaccess is just a file extension.

The file itself is a simple text file edited using a text editor like notepad (not word) with a single directive (instruction) per line.

There can be multiple .htaccess files on a website but only one per directory.

Instructions in a .htaccess file in a parent directory will affect the sub directory unless specifically blocked or overwritten by a .htaccess file in the sub directory.

That is to say a child directories automatically inherits the .htaccess file directives from the parent directory, as the sketch below attempts to show.

htacess file inheritance

Editing the .htaccess file

You can edit the file using any simple text editor.

Editing the file isn’t difficult, however you should be careful when using/editing this file as incorrect editing can cause sever errors.

After any changes you should always take a look at your site in a browser to make sure it loads ok.

In order to help then you may want to use a .htaccess editing tool.

Because Windows systems want to see a file name before the extension you may get error messages trying to create the file.

Here is a video on how to create a .htaccess file on Windows.

Troubleshooting .htaccess file Changes

If you make changes to the .htaccess file and those changes have no effect the most common cause is that the server has been configured to ignore it.

This is done using AllowOverride directive.

Apache guidelines suggest adding garbage to the file and reloading a web page. This should generate a server error. If it doesn’t then the serve has been configured to ignore the .htaccess file.

Here is what Apache say about the AllowOverride directive.

When this directive is set to None, then .htaccess files are completely ignored. In this case, the server will not even attempt to read .htaccess files in the filesystem.

When this directive is set to All, then any directive which has the .htaccess Context is allowed in .htaccess files.

Individual directives can also be allowed or disallowed using the AllowOverride directive.

Related Tutorials and Resources:



Did this Tutorial Help? Please Rate
[Total: 4 Average: 3.8]