Difference between revisions of ".htaccess"

From Christoph's Personal Wiki
Jump to: navigation, search
(External links)
Line 103: Line 103:
  
 
*[http://www.javascriptkit.com/howto/htaccess.shtml Easy to understand tutorial]
 
*[http://www.javascriptkit.com/howto/htaccess.shtml Easy to understand tutorial]
 +
* [http://www.javascriptkit.com/howto/htaccess.shtml Comprehensive guide to .htaccess]
 
*[http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html Documentation for mod_rewrite], frequently used in .htaccess files
 
*[http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html Documentation for mod_rewrite], frequently used in .htaccess files
 
*[http://httpd.apache.org/docs/mod/directive-dict.html#Context Apache configuration directives allowed in .htaccess context]
 
*[http://httpd.apache.org/docs/mod/directive-dict.html#Context Apache configuration directives allowed in .htaccess context]

Revision as of 18:34, 15 September 2006

.htaccess (Hypertext Access) is the default name of Apache's directory-level configuration file. It provides the ability to customize configuration directives defined in the main configuration file. The configuration directives need to be in .htaccess context and the user needs appropriate permissions.

Common usage

Custom Error Pages

ErrorDocument 404 my404page.html
This code can be used to create any custom page. Certain pages are more complicated to modify - if you create a custom 404 Forbidden page, then a viewer will not see the custom page. Here is an example showing a method used to get around this:
ErrorDocument 404 /public/my404page.html
In this case you would need another .htaccess file in /public, containing these lines:
Order allow,deny
Allow from all

Password protection

Make the user enter a name and password before viewing a directory.

AuthUserFile /home/newuser/www/stash/.htpasswd
AuthGroupFile /dev/null
AuthName "Protected Directory"
AuthType Basic
<Limit GET POST>
require user newuser
</Limit>

Now run this command to create a new password for the user 'newuser'.

htpasswd /home/newuser/www/stash/.htpasswd newuser

Password unprotection

Unprotect a directory inside an otherwise protected structure:

Satisfy any

Enable SSI

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

Deny users by IP address

Order allow,deny
Deny from 123.45.67.8
Deny from 123.123.7
Allow from all
This would ban anyone with an IP address of 123.45.67.8 and would also ban anyone with an IP address starting in 123.123.7: for example, 123.123.74.42 would not gain access.

Change the default directory page

DirectoryIndex homepage.html
Here, anyone visiting http://www.example.com/ would see the homepage.html page, rather than the default index.html.

Redirects

Redirect page1.html page2.html
If someone was to visit http://www.example.com/page1.html, they would be sent (with an HTTP status code of 302) to http://www.example.com/page2.html

Prevent hotlinking of images

The following .htaccess rules use mod rewrite.

From specific domains

RewriteEngine on
RewriteCond %{HTTP_REFERER} ^http://([^/]+\.)?baddomain1\.com [NC,OR]
RewriteCond %{HTTP_REFERER} ^http://([^/]+\.)?baddomain2\.com [NC,OR]
RewriteCond %{HTTP_REFERER} ^http://([^/]+\.)?baddomain3\.com [NC]
RewriteRule \.(gif|jpg)$ http://www.example.com/hotlink.gif [R,L]

Except from specific domains

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?example.com/.*$ [NC]
RewriteRule \.(gif|jpg)$ http://www.example.com/hotlink.gif [R,L]
Unless the image is displayed on example.com, browers would see the image hotlink.gif.

Note: Hotlink protection using .htaccess relies on the client sending the correct "Referer" value in the http GET request. Programs such as Windows Media Player send a blank referrer, so that attempts to use .htaccess to protect movie files for example are ineffective.

Standardise web address

RewriteEngine on

RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule (.*) http://www.example.com/$1 [R=Permanent]

If anyone types in your sites address without the 'www' prefix, this will redirect them to the page with the 'www' prefix

Directory rules

A .htaccess file controls the directory it is in, plus all subdirectories. However, by placing additional .htaccess files in the subdirectories, this can be overruled.

User permissions

The user permissions for .htaccess are controlled on server level with the AllowOverride directive which is documented in the Apache Server Documentation.

Other uses

Some web developers have modified .htaccess to perform custom tasks server-side before serving content to the browser. Developer Shaun Inman shows it is possible to edit .htaccess to allow for Server Side Constants within CSS.

See also

External links