This is particularly useful if you have a directory cram packed of images or files that you do not want visitors to directly access. Since all web servers are setup differently, first check to see if your server allows or denies directory listings. If your server allows directory listing but you wish to change that, the respective line in your .htaccess file should look like this:
The *, or asterisk, instructs the server to ignore absolutely everything within the directory. Using the above code, then, will not display any files that are within the directory.
But hey, what happens if your server has directory browsing turned off but you would like it turned on within a directory or two? The line within your .htaccess file should look like this:
This code simply adds an index (file) listing to the directory. Simple enough, right? Getting a tad more detailed, what if you want to only display particular file types? Htaccess gives you the ability to specify particular file extensions to ignore. For example, this code will ignore all files ending in .jpg, .gif, .png, and .txt:
IndexIgnore *.jpg *.gif *.png *.txt
Again, the * wildcard is used to specify any file before that particular extension. So, the above line of code will display every file within the directory except those ending in the 4 extensions.
Making Our Own Index Pages Index pages are those pages that are automatically loaded whenever a file is not explicitly called for within a directory. Take the following as an example:
Because no web page was specifically called (there is no page listed after content1), the web server will load the default page, such as index.html, or default.html. But, being the quite demanding person that you are, what if you want another file to load as default?
If that line of code was placed within the .htaccess file within the content1 directory, then the web server would look for yourcoolsite.com/content1/yoursite.html if it exists. If it does not exist, it will display the directory listing of the directory (unless, of course, directory browsing is turned off).
Still not satisfied? Okay, we can specify more than one index file, taking priority from left to right. Take the code below:
Reading from left to right, if yoursite.html is not found, the server will look for the index.pl file within the cgi-bin directory (from root). If that is not found, then the root index.html file will be loaded. Again, if none of the files are found, a directory listing (unless you specified IndexIgnore *) will be displayed.
Remember that all directories below your present working directory will take this .htaccess file and, accordingly, the DirectoryIndex code that you just wrote, which you may or may not want. If you do not want other directories to take those configurations, simply upload a blank .htaccess file within the respective directory (you can populate the additional .htaccess file if you like).
The first line of code tells the server to support server-side HTML. The 2nd two lines of code add the file extensions that should support server-side HTML. Usually, we only specify .shtml. However, what if you have a million .html files and don't want to rename them all? Our third line of code takes care of that little problem, which instructs the server to interpret all files ending with .html as server-side documents. It is fine, by the way, if some of your .html documents do not have server-side code within them.
The last line of jargon specifies options included when using SSI. You do not need to worry about that line, other than including it in your .htaccess file.
Redirecting The .htaccess file can also be used to establish redirections from one file to another. Redirection is extremely trivial and is setup like this within your file:
Redirect /oldfile.html /newfile.html
Simply, whenever the web server encounters a request for oldfile.html within the root directory, it will forward the request and return the results from newfile.html, also within the root directory.
That is all there is to it to setup .htaccess redirections.
Banning User Access As a web site owner, you may have come across a user who insists on causing trouble. The .htaccess file provides a way to completely deny a user's ability to use your web site, based on the user's IP address or domain name. Let us take a look at how to implement something like this:
order allow,deny deny from 126.96.36.199 deny from really-bad-domain.net allow from all
The above line of code will deny users to the directory in which the .htaccess file is placed with an IP address of 188.8.131.52 or from the domain name really-bad-domain.net. Alternatively, we could deny access to everyone except those browsing from a particular IP address or domain name:
order deny,allow allow from 184.108.40.206 allow from really-nice-domain.net deny from all
This would only allow access to those coming from the 220.127.116.11 IP address or the really-nice-domain.net domain. The order argument determines what order each directive is interpreted. For example, 'order allow,deny' means that the allow directive will be executed first and deny will be executed second.