Apache .htaccess Guide, Tutorials and Articles
Username:
Password:
Register / My Account / Forgotten Password?

How to use .htaccess

(Edit this article)
'.htaccess' is the filename in full, it is not a file extension. For instance, you would not create a file called, 'file.htaccess', it is simply called, '.htaccess'. This file will take effect when placed in any directory which is then in turn loaded via the Apache Web Server software. The file will take effect over the entire directory it is placed in and all files and subdirectories within the specified directory.

You can create a .htaccess file using any good text editor such as TextPad, UltraEdit, Microsoft WordPad and similar (you cannot use Microsoft NotePad).
 
Here is an example of what you might 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

This is a fairly advanced example: it enables password protection on the directory; it offers redirection to a custom error page if a user fails to login correctly; and it enables SSI (server side includes) for use with '.html' files. Please don't be put off, it's very simple once you gain a basic understanding and this article provides examples which are ready to go - simply copy, paste and customise. Examples are explained line by line so it is clear exactly what each line does and why you need it.

Once you have created a .htaccess file, which may look similar to the one shown above (or may simply contain one line), you need to upload it. This should be done using a FTP (file transfer protocol) program. You should already have one which you will have used to upload your web site content. If not, many are available free of charge from web sites such as 'Download.com' and we can recommend 'CuteFTP' and 'WSFTP'.

When uploading your .htaccess files, it is very important you upload the file in 'ASCII' mode. 'ASCII' and 'BINARY' are different methods of transferring data and it is important .htaccess files are transferred in 'ASCII' mode and not 'BINARY'. It is likely your FTP software will default to 'BINARY' so look for a 'Transfer Mode' or 'Transfer Type' option in the menus.

Upload the .htaccess file to the directory you would like it to take effect over. Now visit this directory using your web browser as you would for any other document on your web site and check it has worked correctly.

Note, when you upload your .htaccess file it may not appear in the directory listings for files on your web site. Do not worry; this means your server or FTP software is hiding them which should not be an issue.

A possible cause of error is if the file permissions on the .htaccess file are not set correctly. This only occurs on certain servers, but you may like to change the permissions on the file to '755' or 'executable'. You can do this with your FTP software, look for a 'File Permissions' or 'CHMOD' option, and input '0755'.

If your .htaccess file does not work, you should contact your system administrator or web hosting company and ensure they have enabled .htaccess within your account. Some web hosting companies do not allow use without permission. If errors persist, consult this article for advice, or contact your system administrator for advice.

Updated: 26th May 2007

<- What is .htaccess? Error documents ->

Visitor Comments and Responses

Moderator Article Rating: 10 out of 10
indig10, you can try setting the permissions back to 644 or similar, this should not be a problem. If you receive an error just set the permissions back to 755...
Space Holder
indig10 Article Rating: 10 out of 10
wonderful article! but do you keep the permissions set at 755 or set them back to something else later?
Space Holder
Moderator Article Rating: 10 out of 10
Gary Grady, actually WordPad allows you to save using other file extensions, or you can rename the file afterwards. The issue with NotePad is not in saving the file, it's that it does not support the Unix line return and uses the Windows line return character which causes issues in the .htaccess file - this may not be an issue in more recent versions of NotePad.
Space Holder
Gary Grady Article Rating: 10 out of 10
I think you mean that you shouldn't use Windows Wordpad to edit a .htaccess file, since it's for formatted text (*.doc files). Notepad, a plain text editor, should be OK.
Space Holder
Anonymous Article Rating: 10 out of 10
What a great article, I've learnt everything I needed to know about .htaccess in one short read! Thanks!
Space Holder
Average Rating: 9.11 out of 10 (19 Votes)

Submit Comment/Rating

Article Rating:
Name: (optional)
Your Comment/Response: (optional)
Powered by OpenCrypt Article Manager
Table of Contents RSS Feed
1. What is .htaccess?
2. How to use .htaccess
3. Error documents
4. Redirects
5. Password protection
6. Deny visitors by IP address
7. Deny visitors by referrer
8. Hot link prevention techniques
9. Blocking offline browsers and 'bad bots'
10. DirectoryIndex uses
11. Adding MIME types
12. Enable SSI with .htaccess
13. Enable CGI outside of the cgi-bin
14. Disable directory listings
15. Setting server timezone
16. Changing server signature
17. Preventing access to your PHP includes files
18. Prevent access to php.ini
19. Forcing scripts to display as source code
20. Ensuring media files are downloaded instead of played
21. Setting up Associations for Encoded Files
22. Preventing requests with invalid characters
23. Useful Resources
 
Sponsors
Space Holder
Space Holder


Copyright © 1999 - 2008 ionix Limited. All Rights Reserved. Company Information, Affiliates

Powered by OpenCrypt Membership Software