Home arrow Apache arrow Page 3 - 7 Powerful .htaccess Customization Tips

7 Powerful .htaccess Customization Tips

Turn your web site into the ultimate stealth machine using Steve's 7 powerful .htaccess customization tips, which he reveals in this article.

Author Info:
By: Steve Adcock
Rating: 5 stars5 stars5 stars5 stars5 stars / 13
November 28, 2002
  1. · 7 Powerful .htaccess Customization Tips
  2. · .htaccess Naming Conventions and File Creation
  3. · Modifying Directory Indexes
  4. · Password Protection
  5. · Conclusion

print this article

7 Powerful .htaccess Customization Tips - Modifying Directory Indexes
(Page 3 of 5 )

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:

IndexIgnore *

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:

Options +Indexes

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?

DirectoryIndex yoursite.html

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:

DirectoryIndex yoursite.html /cgi-bin/index.pl /index.html

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).

Permit SSI Extensions
SSI extensions allow server-side HTML, which are little snippets of code that run on the server-side, rather than on the client-side like HTML and Javascript. To enable SSI within your htaccess file, include this code anywhere within it:

AddType text/html .shtml
AddHandler server-parsed .shtml
AddHandler server-parsed .html
Options Indexes FollowSymLinks Includes

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.

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.

Want to redirect to another site? No problem:

Redirect /oldfile.html http://www.yournewsite.com/newfile.html

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
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 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
allow from really-nice-domain.net
deny from all

This would only allow access to those coming from the 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.
blog comments powered by Disqus

- Programmatically Manipulating Microsoft Exce...
- Installing PHP under Windows
- Compressing Web Content with mod_gzip and mo...
- Compressing Web Output Using mod_deflate and...
- Setting Up Apache 2.0.45 to Parse PHP Pages
- Custom Error 404 Documents with PHP
- Using Apache and PHP on Mac OS X
- ASP: Active Sessions, Active Logins and Tota...
- Working With Oracle on Windows: Part 1
- The Quick-n-Dirty Guide to Setting Up Apache...
- Installing Apache With SSL: The Complete Gui...
- 7 Powerful .htaccess Customization Tips
- Trap And Get Notified: A Practical Solution ...
- One Way To Use Server Side Includes
- Using ForceType For Nicer Page URLs

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials