Difference between revisions of ".htaccess"

From HalfgeekKB
Jump to navigation Jump to search
 
Line 29: Line 29:
  
 
This all depends on which MIME types (and pseudo-types) the server has set up.  <code>cgi-script</code> is usually the type for CGI scripts.
 
This all depends on which MIME types (and pseudo-types) the server has set up.  <code>cgi-script</code> is usually the type for CGI scripts.
 +
 +
=mod_rewrite:  Pretend the user typed some other URL...=
 +
 +
[[mod_rewrite]] is a particularly useful tool for tricking the user agent.  The point is to allow a user to access a URL by using another.  The net effect is almost the same as a redirect, except that no redirect happens&mdash;the correct content is automatically passed through!  See [[mod_rewrite]].

Revision as of 13:45, 30 September 2005

Some defaults

Options +ExecCGI +MultiViews -Indexes

ExecCGI allows CGI programs to be run in this directory.

MultiViews allows Apache to process a request by guessing a file extension; i.e., http://example.com/images/first could lead to either http://example.com/images/first.jpg or http://example.com/images/first.png, whichever is available.

Indexes allows web users to see the directory listings in a directory.

Permissions

The Files directive can be used to conveniently hide files that ought not be visible to the outside world. This directive hides Perl modules and POD documents in this directory and all subdirectories (excepting perhaps subdirectories with their own .htaccess files):

<Files ~ "\.(pm|pod)$">
  Order allow,deny
  Deny from all
</Files>

Note that if you really want to make sure a file stays hidden on the web, prefix its filename with ".ht". Apache comes standard making all files that begin with .ht invisible (using the same mechanism as above, actually).

ForceType: Pretend a file has a different type

If you'd like to create a custom file extension for, say, a PHP or CGI script, ForceType is a nice tool. To use this to force files with the extension .pro to be used as (Apache-executed) PHP scripts, something like this should work:

<Files ~ "\.pro$">
  ForceType application/x-httpd-php
</Files>

This all depends on which MIME types (and pseudo-types) the server has set up. cgi-script is usually the type for CGI scripts.

mod_rewrite: Pretend the user typed some other URL...

mod_rewrite is a particularly useful tool for tricking the user agent. The point is to allow a user to access a URL by using another. The net effect is almost the same as a redirect, except that no redirect happens—the correct content is automatically passed through! See mod_rewrite.