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:
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:
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.
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/ http://www.yourdomain.com/new_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 "Hoo.st Authentication"
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: http://www.htaccesstools.com/htpasswd-generator/
Changing the index file
Another interesting feature is the changing of the index file or the order of index files. By default the Hoo.st servers search for the index files: index.php and index.html. To change, add in htaccess:
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: