October 28, 2013 Vinícius Muniz


What is .htaccess?

.Htaccess is a configuration file for directory. In it, you can override the default settings of the server, such as directory listings, access permissions, standard file index, among others.

How to use .htaccess?

Easy, the .htaccess is simply a file. If you want some functionality available on your hosting server, go to the folder you want this specific configuration and create the file “.htaccess”.

In this file, write the desired functionality, so the directory where you created the .htaccess and all child directories will inherit that setting.

And if you do not want that configuration in a specific folder, you can disable it with another .htaccess file.

Lets see a practical example:

I have a folder /home/vinicius/public_html and i want the files and directories to be listed. Then add the .htacess file with the content:

Options +Indexes

However, inside this folder i have the folder Scriptcase that i don’t wanted to be listed. So, add another .htaccess in that folder, with the content:

Options -Indexes

Remember that if an index file exists in the directory (by default, a file named index.html or index.php), these files will be displayed, discarding the directory listing. Below we will see how to change the index file.

Main directives


Redirect allows us to direct visitors from a document to another one. This is useful, for example, if you move the contents of the site and would like to redirect visitors from old links to the new site content. Add to your htaccess:

Redirect /old_directory/

Protecting a directory

Password protection and authentication systems offered by the Web Server is probably the most important use of .htaccess. Very easily, we can password protect a directory (or several) to require a username and password to open it. Example:

AuthName " Authentication"
AuthUserFile /home/vinicius/.htpasswd
AuthType Basic
require valid-user

Explaining each line:

  • AuthName: The message will appear as soon as the visitor tries to access your folder.
  • AuthUserFile: File which will contain a username and password for authentication.
  • AuthType: Authentication type.
  • require: The condition to access the folder or folders.

In the file .htpasswd, you must put the username and password encrypted.

For the creation of this file you can use this information:

Changing the index file

Another interesting feature is the changing of the index file or the order of index files. By default the servers search for the index files: index.php and index.html. To change, add in htaccess:

DirectoryIndex home.html

The htaccess has many features, including use of regular expressions to redirect, deny access, among other features. You can find many other features on the links:

Receba Mais noticias!

Vinícius Muniz
Vinícius Muniz

Systems developer, user and Scriptcase Manager

Back to Topback to top