HT: Difference between revisions
(revision 2) |
(Removed excess category tags; Additional grammar and cleanup edits.) |
||
(7 intermediate revisions by 5 users not shown) | |||
Line 1: | Line 1: | ||
This article will help you understand and | This article will help you understand some aspects and advantages of the .htaccess file you see when you run an Apache Web Server like ours. | ||
== | ==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 the web server operates. When it is placed in a particular directory, the rules in it apply to that directory and all the subdirectories thereof. | |||
Here is a [http://www.javascriptkit.com/howto/htaccess.shtml good tutorial] on the use and configuration of the .htaccess file. | |||
==What is a .htpasswd 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 | 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== | ==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 change the default index file, which is normally index.html, index.ext... to anything else, like foo.ext or whatever name/extension you prefer. To do this, use: | |||
<pre>DirectoryIndex foo.ext home.html home.php foo.php</pre> | <pre>DirectoryIndex foo.ext home.html home.php foo.php</pre> | ||
===Customizing Error Handling/Error Pages=== | |||
If you have ever wondered how people change their 404, 500... error pages to something like lost.ext, then you will like this code in your .htaccess file: | |||
<pre> | <pre> | ||
<nowiki> | <nowiki> | ||
Line 19: | Line 24: | ||
</nowiki> | </nowiki> | ||
</pre> | </pre> | ||
Where [Error Number] is replaced with the error number, and [Error Document] is replaced with the path | Where [Error Number] is replaced with the error number, and [Error Document] is replaced with the path to the error document, which can be internal or external, as in: | ||
<pre>http://www.anothersite.com/foo.ext or /foo.ext</pre> | <pre>http://www.anothersite.com/foo.ext 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] | <pre>Redirect [Trigger] [New Destination] | ||
Redirect /old http://www.url.com/new | Redirect /old http://www.url.com/new | ||
Redirect /old /new</pre> | Redirect /old /new</pre> | ||
===Limiting Access by Hostname/IP Address=== | |||
Use this section of this article if you are interested in blocking access to a file/folder on your server: | |||
<pre> | <pre> | ||
<Files admin.cgi> | <Files admin.cgi> | ||
Line 32: | Line 41: | ||
allow from 1.2.3.4 | allow from 1.2.3.4 | ||
</Files> | </Files> | ||
</pre> This example denies access to admin.cgi | </pre> This example denies access to admin.cgi for everyone but the user of the IP Address referred in ''1.2.3.4''. 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 interested in using the Hostname rather than the IP then use: | ||
<pre> | <pre> | ||
<Files admin.cgi> | <Files admin.cgi> | ||
Line 40: | Line 49: | ||
</Files> | </Files> | ||
</pre> | </pre> | ||
You can also use it for your whole network to have access to it alone | You can also use it for your whole network to have access to it alone. Example: | ||
<pre> | <pre> | ||
# IP Number | # IP Number | ||
Line 55: | Line 64: | ||
</Files> | </Files> | ||
</pre> | </pre> | ||
Where ''192.168.123'' is your internal network IP and .networkdomain.com 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 | Where ''192.168.123'' is your internal network IP and .networkdomain.com 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 practical example for advanced users: | ||
<pre> | <pre> | ||
<Files [/path/filename]> | <Files [/path/filename]> | ||
Line 61: | Line 70: | ||
</files> | </files> | ||
</pre> | </pre> | ||
===Limiting Access by User=== | |||
This part shows the .htaccess/.htpasswd user login system which uses cookies. It is only partly secure because the session does not expire until all open browsers are closed, so do not use it on a site section that needs fool-proof security. Here is the code: | |||
<pre> | <pre> | ||
AuthType Basic | AuthType Basic | ||
Line 68: | Line 79: | ||
Require valid-user | Require valid-user | ||
</pre> | </pre> | ||
For this example you | For this example, you place 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|The Uniform Server]], then you do not need to do that. You can also use this example to protect another directory by using just one .htaccess file: | ||
<pre> | <pre> | ||
<Directory /path/to/> | <Directory /path/to/> | ||
Line 77: | Line 88: | ||
</Directory> | </Directory> | ||
</pre> | </pre> | ||
If you | If you want to do this for just specific files, then use: | ||
<pre> | <pre> | ||
<Files /path/to/file.ext> | <Files /path/to/file.ext> | ||
Line 86: | Line 97: | ||
</Files> | </Files> | ||
</pre> | </pre> | ||
The ''Require'' statement is used to list valid users or groups of users | The ''Require'' statement is used to list valid users or groups of users. If you just want one .htpasswd file but want multiple protected areas, then you can use: | ||
<pre>Require user username1 username2 username3...</pre> | <pre>Require user username1 username2 username3...</pre> | ||
Or if you want to use it in groups then you can use: | Or if you want to use it in groups then you can use: | ||
<pre> | <pre> | ||
AuthGroupFile / | AuthGroupFile /htgroups/path/to/.htgroups | ||
Require group groupname1 groupname2 groupname3... | Require group groupname1 groupname2 groupname3... | ||
</pre> | </pre> | ||
Line 98: | Line 109: | ||
Groupname2: username1 username4 username5 .... | Groupname2: username1 username4 username5 .... | ||
</pre> | </pre> | ||
As you can see a username may be in as many | As you can see, a username may be in as many groups as you like while others may be in only one. | ||
[[Category: Apache Configuration]] |
Latest revision as of 10:31, 21 June 2013
This article will help you understand some aspects and advantages of the .htaccess file you see when you run an Apache Web Server like ours.
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 the web server operates. 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 on 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 change the default index file, which is normally index.html, index.ext... to anything else, like foo.ext or whatever name/extension you prefer. To do this, use:
DirectoryIndex foo.ext home.html home.php foo.php
Customizing Error Handling/Error Pages
If you have ever wondered how people change their 404, 500... error pages to something like lost.ext, then you will like this code in your .htaccess file:
ErrorDocument [Error Number] [Error Document] Error Document 404 /404.html
Where [Error Number] is replaced with the error number, and [Error Document] is replaced with the path to the error document, which can be internal or external, as in:
http://www.anothersite.com/foo.ext or /foo.ext
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:
Redirect [Trigger] [New Destination] Redirect /old http://www.url.com/new Redirect /old /new
Limiting Access by Hostname/IP Address
Use this section of this article if you are interested in blocking access to a file/folder on your server:
<Files admin.cgi> order deny, allow deny from ALL allow from 1.2.3.4 </Files>
This example denies access to admin.cgi for everyone but the user of the IP Address referred in 1.2.3.4. 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 interested in using the Hostname rather than the IP then use:
<Files admin.cgi> order deny, allow deny from ALL allow from mymachine.networkdomain.com </Files>
You can also use it for your whole network to have access to it alone. Example:
# IP Number <Files admin.cgi> order deny, allow deny from ALL allow from 192.168.123 </Files> # Hostname <Files admin.cgi> order deny, allow deny from ALL allow from .networkdomain.com </Files>
Where 192.168.123 is your internal network IP and .networkdomain.com 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 practical example for advanced users:
<Files [/path/filename]> [Attributes to apply to file...] </files>
Limiting Access by User
This part shows the .htaccess/.htpasswd user login system which uses cookies. It is only partly secure because the session does not expire until all open browsers are closed, so do not use it on a site section that needs fool-proof security. Here is the code:
AuthType Basic AuthName "Restricted Access" AuthUserFile /htpasswd/path/to/.htpasswd Require valid-user
For this example, you place a .htpasswd file in the path (/htpasswd/path/to/). In the .htpasswd file will be:
[user]:[password]
Normally you have to encrypt the password but if you are using The Uniform Server, then you do not need to do that. You can also use this example to protect another directory by using just one .htaccess file:
<Directory /path/to/> AuthType Basic AuthName "Restricted Access" AuthUserFile /htpasswd/path/to/.htpasswd Require valid-user </Directory>
If you want to do this for just specific files, then use:
<Files /path/to/file.ext> AuthType Basic AuthName "Restricted Access" AuthUserFile /htpasswd/path/to/.htpasswd Require valid-user </Files>
The Require statement is used to list valid users or groups of users. If you just want one .htpasswd file but want multiple protected areas, then you can use:
Require user username1 username2 username3...
Or if you want to use it in groups then you can use:
AuthGroupFile /htgroups/path/to/.htgroups Require group groupname1 groupname2 groupname3...
And in the .htgroups file would be:
Groupname1: username1 username2 username3 ... Groupname2: username1 username4 username5 ....
As you can see, a username may be in as many groups as you like while others may be in only one.