From The Uniform Server Wiki
Revision as of 00:59, 24 November 2010 by Upazixorys (talk | contribs)
Jump to navigation Jump to search

Page Is Unavailable Due To Site Maintenance, Please Visit Reserve Copy Page


This article will help you understand and build your knowledge of the .htaccess file you see when you run an Apache Web Server like ours.

Note: Article is still under editing

What is a .htaccess file?

It is Apache's directory-level configuration file (as opposed to httpd.conf, which is the main server configuration file) that provides the governing rules of how a web server should be ran/behave. When it is placed in a particular directory, the rules in it apply to that directory and all the subdirectories thereof.

Here is a good tutorial to the use and configuration of the .htaccess file.

What is a .htpasswd file?

The .htpasswd file is a file used to store usernames and passwords for protected areas of a website that use the .htaccess Protection.

Usage and Commands

Here are some examples as to how they can be used.

Change the Default Directory Index File

It can be used to chnage the default index file which is normally index.html, index.ext... to anything like foo.ext or whatever name/extension you prefer. To do this, use: <pre>DirectoryIndex foo.ext home.html home.php foo.php</pre>

Customizing Error Handling/Error Pages

If you have ever wondered how people chnage their 404, 500... error pages to something like lost.ext, then you will like this code in your .htaccess file: <pre> <nowiki> ErrorDocument [Error Number] [Error Document] Error Document 404 /404.html </nowiki> </pre> Where [Error Number] is replaced with the error number, and [Error Document] is replaced with the path fo the error document which can be internal or external as in: <pre> or /foo.ext</pre>

Server Generated URL Redirects

You moved or renamed a directory and you know people still have the old directory bookmarked so you want them to be redirected to the new directory, then you can use this code: <pre>Redirect [Trigger] [New Destination] Redirect /old Redirect /old /new</pre>

Limiting Access by Hostname/IP Address

Use this section of this article if you are intrested in blocking access to a file/folder on your server: <pre> <Files admin.cgi>

order deny, allow
deny from ALL 
allow from 

</Files> </pre> This example denies access to admin.cgi to everyone but the owner of the IP Address mention in You can also use this for a folder, in that case you would replace admin.cgi with the name of the folder. If you are intrested in using the Hostname rather than the IP then use: <pre> <Files admin.cgi>

order deny, allow
deny from ALL 
allow from

</Files> </pre> You can also use it for your whole network to have access to it alone, example: <pre>

  1. IP Number

<Files admin.cgi>

order deny, allow
deny from ALL 
allow from 192.168.123 


  1. Hostname

<Files admin.cgi>

order deny, allow
deny from ALL 
allow from

</Files> </pre> Where 192.168.123 is your internal network IP and is your Hostname/Domain. You can also switch it to allow from ALL and deny from a list of IPs or Hostnames.Here is a pratical example for advance users: <pre> <Files [/path/filename]>

[Attributes to apply to file...]

</files> </pre>

Limiting Access by User

This part lets you use a .htaccess/.htpasswd user login system that uses cookies. It is not fully safe because the session does not expire until all open broswers are closed so try not to use it much on your site section that needs foul proof security. Here is the code: <pre> AuthType Basic AuthName "Restricted Access" AuthUserFile /htpasswd/path/to/.htpasswd Require valid-user </pre> For this example you places a .htpasswd file in the path (/htpasswd/path/to/). In the .htpasswd file will be: <pre>[user]:[password]</pre> Normally you have to encrypt the password but if you are using The Uniform Server, then you do not need to do that. You ca also use this example to protect another directory from just 1 .htaccess file: <pre> <Directory /path/to/> AuthType Basic AuthName "Restricted Access" AuthUserFile /htpasswd/path/to/.htpasswd Require valid-user </Directory> </pre> If you are intrested in doing this for just specific files then use: <pre> <Files /path/to/file.ext> AuthType Basic AuthName "Restricted Access" AuthUserFile /htpasswd/path/to/.htpasswd Require valid-user </Files> </pre> The Require statement is used to list valid users or groups of users so if you just want 1 .htpasswd file, but want multiple protected areas, then you can use: <pre>Require user username1 username2 username3...</pre> Or if you want to use it in groups then you can use: <pre> AuthGroupFile /htgorups/path/to/.htgroups Require group groupname1 groupname2 groupname3... </pre> And in the .htgroups file would be: <pre> Groupname1: username1 username2 username3 ... Groupname2: username1 username4 username5 .... </pre> As you can see a username may be in as many group as you like while others may just be in 1.