Guide to .htaccess
I had some problems recently with one of my .htaccess files and spent some time to collect more information on how to use it properly.
I summarize below some points.
General Information about .htaccess
According to Wikipedia
“.htaccess (hypertext access) file is a directory-level configuration file supported by several web servers, that allows for decentralized management of web server configuration.
The original purpose of .htaccess – reflected in its name – was to allow per-directory access control, by for example requiring a password to access the content. however, the .htaccess files can override many other configuration settings including content type and character set, CGI handlers, etc.
These files are placed inside the web tree, and are able to override a subset of the server’s global configuration for that directory, and all sub-directories”
An htaccess file is a simple ASCII file, such as you would create through a text editor like Note Pad. htaccess is an Apache file, not an NT thing. There are similar capabilities for NT servers, though in my professional experience and personal opinion, NT’s ability in these areas is severely handicapped. Most commands in htaccess are meant to be placed on one line only, so if you use a text editor that uses word-wrap, make sure it is disabled or it might throw in a few characters that annoy Apache to no end, although Apache is typically very forgiving of malformed content in an htaccess file.
htaccess files affect the directory they are placed in and all sub-directories, that is an htaccess file located in your root directory (lankabusinesspages.com/) would affect lankabusinesspages.com/content, lankabusinesspages.com/content/contents, etc. It is important to note that this can be prevented (if, for example, you did not want certain htaccess commands to affect a specific directory) by placing a new htaccess file within the directory you don’t want affected with certain changes
Error Documents Definition in .htaccess
You can specify pages to be accessed when the web server returns an error code by using a syntax such as:
- ErrorDocument 404 /errors/notfound.html
- ErrorDocument 500 /errors/serverr.html
You can also define HTML as in the following example:
You can also specify HTML, believe it or not!
ErrorDocument 401 “<body bgcolor=#ffffff><h1>You have to actually <b>BE</b> a <a href=”#”>member</A> to view this page!
Password Protection Definition in .htaccess
If you want a specific directory in your site to be available only to some people, you can require the use of a password
by having some lines in your .htaccess file such as the following:
- AuthUserFile /usr/local/you/safedir/.htpasswd
AuthGroupFile /dev/null
AuthName EnterPassword
AuthType Basic
Require user SpecialUser
The user name and password of the user SpecialUser should be specified in another file called:
- .htpasswd
- IP Blocking Definition in .htaccess
- You can block the access to specified IP addresses by entering lines such as the following:
- order allow,deny deny from 123.45.6.7 deny from 012.34.5. allow from all
Redirects Definition in .htaccess
If you make changes to your website, you might want to redirect a user to a new page, whenever he tries to access an old page. You can do that by using redirect instructions such as the following:
Redirect /olddirectory/oldfile.html http://yoursite.com/newdirectory/newfile.html
Conclusions
We have discussed above just a few possible uses of the .htaccess file. There is a list of Apache Directives you can use for your htaccess files, even if not all of them are designed to be used by htaccess. You should also go through the Apache User’s Guide for more detailed information if you are really serious about making your life easier as a webmaster.