Apache allows webusers to create special files called .htaccess with which access control and a range of apache defaults can be managed by the user itself. It basically allows you to manipulate the behaviour of the server.
Why .htaccess?
The reason .htaccess has such a name, and no file-type suffix, is to differentiate it from ordinary files, and to make sure that we do not create a file of that name by accident.
What is .htaccess mainly used for (incl. solutions)?
Let us consider the most common instances;
a) To create custom error pages/redirects for server errors
e.g. “404 Not Found”
Sample Solution –
Put the following line, or its modification in .htaccess;
ErrorDocument 404 /cgi-bin/404.cgi
b) To protect any part of the website, so that only proper users with valid usernames and passwords can access it. The .htaccess file specifies the limit of the protection only, it does not contain the actual username and password. These are stored in a different file called .htpasswd.
Sample Solution –
Put the following lines in .htaccess;
AuthName “Title for the Protected Area ”
AuthType Basic
AuthUserFile /path/to/.htpasswd
Put the username and password in the .htpasswd file;
samplename:S@mPLepAsWoRd
Even though the .htpasswd file requires encrypted passwords, the user must enter them in clear text.
c) To change the way file extensions are normally treated. Usually, the server has a set of standard file types that it recognizes, but this changes that, and the way files are treated.
Sample Solution –
Put the following line, or its modification in .htaccess;
AddType application/x-httpd-php .php .htm .html
This prompts the system that files with the extension .html, .htm, and files with the suffix .php, all be treated as standard HTTP documents containing PHP.
d) To change the default file name in a directory, so that it does not have to be called index.html or index.extenstion, but we can change it to call whatever we like.
Sample Solution –
Put the following line, or its modification in .htaccess;
DirectoryIndex mypage.html myindex.php index.html index.htm
e) To allow / prevent directory listings. This is mainly useful when we have an index page missing, and want to have other files remain invisible (prevent listing), OR when we want to upload data to the
server, and view its content from the browser.
Sample Solution –
Put the following line, or its modification in .htaccess;
To allow directory listing –
Options All MultiViews
To use normally when index file is missing –
Options +Indexes
To prevent directory listing –
Options -Indexes
f) To change or add file description and type information. If you have a file with a file type that is not recognized by the system, we can setup a file description to distnguish this from other file types.
Sample Solution –
Put the following line, or its modification in .htaccess;
AddDescription “Working filetype” .test
Hope this was short but descriptive enough to understand what an .htaccess basically does.
In other words .. .htaccess rocks!!