.htaccess - That File Can Be Tricky
Apache is the the most popular webserver environment in existance. It’s free, fast and provides stability next to none. Within Apache’s framework is a file called .htaccess
.htaccess is actually the extension of the file. The file doesn’t have a name, just an extension, never more. From here on out I will just refer to it as “htaccess”.
htaccess is a distributed configuration file meaning it has the ability to modify the apache configuration on a per-directory basis. The htaccess file controls the folder it is located in as well as any subfolders. You can have other htaccess files in the subdirectories to perform special actions on them but you have to be careful. You could potentially create an infinite loop if the configuration in one htaccess file conflicts with one in another folder. The reason why is a bit easier to understand once you realize what htaccess can do and when it should/should not be used.
When not to use htaccess:
(from Apache’s Tutorial pages)
“In general, you should never use
.htaccessfiles unless you don’t have access to the main server configuration file.”
Why you may ask? htaccess can modify the server configuration file (called httpd.conf in Apache) In fact, you have to enable the use of htaccess in the configuation file before you can use htaccess to modify the configuration. Confused? Lets see if taking a step back helps. In the Apache configuration file (httpd.conf) there in an option called “AllowOverride”. If this option is set to “none” then an htaccess file, even if it exists, is ignored. You have to tell htaccess what it can modify by listing what it is allowed to do using the “AllowOverride” setting. Once this is setup you can use the htaccess file to modify anything that is explicitly stated by the “AllowOveride” setting in the main Apache configuration file.
In a shared hosting environment the hosting company is in charge of the Apache configuration file and they usually enable the “AllowOverride” setting for certain functions so users can do some basic configuration of the site. The most common uses of htaccess are password protection of directories and redirects of webpages. The syntax used in the htaccess file can be confusing for even experienced computer users and many time can be quite complex. For example, the code below would provide a redirect from http://domain.tld to http://www.domain.tld:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^YourSite\.com [nc]
RewriteRule (.*) http://www.YourSite.com/$1 [R=301,L]
Many people who run their own website could benefit from the use of an htaccess file for many reasons but do not have a clue how to write their own. If you have ever wanted to:
- Create a redirect
- Password protect part of your site
- Direct to custom error pages
- Adjust the caching of particular files
- A bunch of other stuff…
…then htaccess can do it for you. The best part is you don’t have to write it yourself. Instead, use the htaccess generator from cooltips.de It will save you a lot of time an frustration. You may have to check with your hosting company about what parameters they allow to be modified within htaccess.
Popularity: 7% [?]
Filed under: F.Y.I.









