A text file called .htaccess can be used to control web server (Apache) behavior for your web site. The other sections of this .htaccess tutorial deal with actual commands (called directives) which can be used.


What are .htaccess files?
.htaccess files are plain text files you can use to make configuration changes to your server. Each line in a .htaccess file is called a directive. Directives are applied to the directory the .htaccess file resides in and any subdirectories within that directory.

Creating A .htaccess File

Creating a .htaccess file may cause you a few problems. Writing the file is easy, you just need enter the appropriate code into a text editor (like notepad). You may run into problems with saving the file. Because .htaccess is a strange file name (the file actually has no name but a 8 letter file extension) it may not be accepted on certain systems (e.g. Windows 3.1). With most operating systems, though, all you need to do is to save the file by entering the name as:


.htaccess"

(including the quotes). If this doesn't work, you will need to name it something else (e.g. htaccess.txt) and then upload it to the server. Once you have uploaded the file you can then rename it using an FTP program.

Using .htaccess files

To put a .htaccess file to work, you have to upload it to your server.

Before uploading a .htaccess file, always make sure there's isn't one already in the directory you're uploading it into. This can be tricky as many servers are configured to hide files with names that begin with a period. Here's how to set up your FTP client to show .htaccess files.

Once you've checked for an existing .htaccess file, you can upload your file (in ASCII mode) into the directory you want to work with, overwrite the existing .htaccess file, or add the directives from your .htaccess file to the one that's already on your server.

Is .htaccess enabled?


It's unusual, but possible that .htaccess is not enabled on your site. If you are hosting it yourself, it's easy enough to fix; open your httpd.conf in a text editor, and locate this <Directory> section..

Your  DocumentRoot may be different, of course..

# This should be changed to whatever you set DocumentRoot to.
 #
 <Directory "/var/www/htdocs">
 #

..locate the line that reads..
AllowOverride None
..and change it to..
AllowOverride All

Restart Apache. Now .htaccess will work. You can also make this change inside a virtual host, which would normally be preferable.

 If your site is hosted with someone else, check your control panel (Plesk. CPanel, etc.) to see if you can enable it there, and if not, contact your hosting admins. Perhaps they don't allow this. In which case, switch to a better web host.


What can you do with .htaccess?
  • Well, you can reach all sorts of things. Like:
  •  Make own error pages.
  •  Protect directories with .htaccess and .htpasswd.
  •  Choose for www for you domain or not.
  •  Make a redirect.
  •  Block IP-addresses.
  •  Set your index page.
  •  Avoid directory browsing.
  •  A HTML file parse as PHP file.
  •  Avoid hot linking
  •  Mod-Rewrite