What is .htaccess?

Simply speaking, .htaccess is a configuration file that can alter Apache server’s behavior. But this simple definition overlooks most of its capabilities hence a moderate level of analysis is needed.

htaccess rewrite rules

  • .htaccess is a configuration file for use on web servers running the Apache Web Server software.
  • it can be used to alter the configuration of the Apache Web Server software.
  • it provide a way to make configuration changes on a per-directory basis.
  • .htaccess file containing one or more configuration directives, if placed in a particular document directory, the directives will be applied to that directory and all its subdirectories too.

Suppose, your project directory structure deployed in a server is as follows:




You have written a bunch of directives in an .htaccess file to be applied on all .php files under modules directory. The .htaccess file is placed in modules directory, but its effect will reflect on all .php files in modules as well as all php files in subdirectories under modules.

  • It is also called "distributed configuration files".
  • Many a times, the server administrator is not willing to make frequent configuration changes, but permits individual users to make these changes via .htaccess files for themselves.

For example, many web hosting companies provide shared hosting plan. If you are a developer/blogger playing with this, you already know they do not allow their customers to access the global configuration file directly as it may affect other customers hosted on that server. Instead, they provide a mean to configure Apache server for their own directories and subdirectories through .htaccsess.

Note:  “Directives” are commands or instructions used for modifying Apache server’s behavior.

How directives are applied?

  1. The configuration directives found in a .htaccess file are applied to the directory in which the .htaccess file is found, and to all subdirectories thereof.
  2. However, it is important to remember that there may have been .htaccess files in directories higher up. Directives are applied in the order that they are found. Therefore, an .htaccess file in a particular directory may override directives found in .htaccess files found higher up in the directory tree. That is, in the case of multiple .httaccess files, the directives of the innermost one will win the race.

An .htaccess offers a bunch of powerful features & functionalities that we can exploit several ways. These are great tools for developers as well as bloggers. It allows developers to execute a bunch of directives without requiring access to Apache’s core server configuration file.

How to create .htaccess file?

Creating .htaccess file is very simple. You can create it using Notepad, Notepad++ ,  Netbeans IDE  keeping the only thing  in mind to name the file .httacess without any other extension. Simply open Notepad and save the file as .htaccess. Once you are done you can upload it to server using ftp client like FileZilla.

Where to place .htaccess file on web server?

The placement of the .htaccess file is important as its effect may vary based on where it is placed. The file can be placed in any directory or subdirectory for which changing configuration is needed. If .htaccess file is placed in root directory, it will affect that directory as well as its subdirectories. On the other hand, if .htaccess file is placed in subdirectory it will affect the subdirectory, side by side override directives or commands of .htaccess files placed in higher ups.

How to enable .htaccess?

By default, almost all hosting companies allow their customers to execute directives in .htaccess file on their servers, so nothing to worry about this. But, if you need to configure .htaccess on your local server you have enable mod_rewrite extension in php.ini file in Apache Web Server configurations.

What can you do with an .htaccess file?

Simply a lot, I am listing a few of that

  1. Generate SEO friendly URLs also known as clean URLs
  2. Default directory Listing
  3. Disable directory Listing
  4. Error Pages handling
  5. Hiding File Extension
  6. Domain Redirection
  7. Old Domain Redirection
  8. Server Side Includes (SSI)

And so on.

Rewire rules & rewriting urls:

As name implies, rewrite rules are rules rewritten for overriding existing rules. The ability to rewrite urls is one of the powerful features of .htaccess that enables us to manipulate urls, transforming some long ugly URLs into clean SEO friendly URLs.


http:// yoursite.com/home RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?param=$1

Beginning rewriting

Step1: check whether mod_rewrite module is on

<IfModule mod_rewrite.c>

Step 2: You only need to do this once per .htaccess file to turn rewrite engine on

Options +FollowSymlinks
RewriteEngine on

Step 3:  Write your rewrite rule

RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?param=$1 [NC,L]

Your final .htaccess file

<IfModule mod_rewrite.c>
 Options +FollowSymlinks
RewriteEngine on RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?param=$1 [NC,L] </IfModule>

NOTE: Apache recommends to use .htaccess when

  1. You don’t have access to httpd main server config file and if you have access to httpd config file it is fine to avoid .htaccess. Using .htaccess files slows down your Apache http server.
  2. .htaccess files should be used in a case where the content providers need to make configuration changes to the server on a per-directory basis, but do not have root access on the server system.
Check Page Rank