The /.htaccess
file communicates options to the server. It is documented on the Apache site.
.htaccess
also serves to supply environment variables to php
scripts. The syntax is
SetEnv variable value
The script fetches the value with $_SERVER['variable']
The following is an annotated version of the distributed .htaccess file.
Options -Indexes
DirectoryIndex index.php index.html |
The first line declares that the server should never serve a directory listing. Instead, the second line dictates that when a directory is requested, the server should serve up its index.php or index.html , if either can be found. Otherwise the server signals a 404 error, which is processed as described below. |
SetEnv JAVAROOT DocRoot/Java
SetEnv SRCROOT DocRoot
SetEnv PICTOOLS DocRoot/pictools
|
Set environment variables. DocRoot must be replaced with what will be the value of DOCUMENTROOT . For PhysPics it is /home/physpics/physpics.com |
AddType application/x-java-jnlp-file .jnlp
AddType text/x-java-source .java
AddType application/java-archive .jar |
Each AddType line gives the MIME type for serving files that end in a specific extension. Browsers interpret the MIME type to determine how to treat the incoming file. Beyond this, the server has no control of what the browser does with the file. Typically browsers try to execute .jnlp files and save to disk .java files and .sar files. |
ErrorDocument 400 /error.php
ErrorDocument 401 /error.php
ErrorDocument 402 /error.php
ErrorDocument 403 /error.php
ErrorDocument 404 /admin/urlHandler.php
ErrorDocument 405 /error.php
...
#ErrorDocument 500 /error.php
#ErrorDocument 501 /error.php
... |
An ErrorDocument line specifies a page to serve for a specic kind of error. Mostly these lines call for serving error.php, but none of these errors are common. The most frequent error is 404 which means the client request is for a page that does not exist. These errors are handled with urlHandler.php, which may redirect the page or decide that the request really is an error. These sorts of errors are announced via box404. |
Most of
.htaccess
is rules for
mod_rewrite
, which modifies incoming URLs to map from requested pages to existing pages. The usage here is minimal; most of the functionality I wanted is embodied in
urlHandler.php
. One advantage is that I can count requests for previously moved files.
RewriteEngine On
RewriteBase / |
Turn on mod_rewrite . All its rules begine with "Rewrite ". |
RewriteRule (^|/)\. error.php [R=403,L] |
Disallow files or directories whose names start with ".". |
RewriteRule (^|.*/)Pictools\.[^/]+$ \
error.php [R=403,L] |
Disallow access to .../Pictools.extenxion . |
RewriteRule ^$ index.php [L]
RewriteCond %{DOCUMENT_ROOT}/$1/index.php -f
RewriteRule ^(.*)/$ $1/index.php [L] |
Succeed if want a directory containing index.php . |
RewriteRule .php$ - [NC,S=13]
RewriteRule .html$ - [NC,S=12]
...
RewriteRule .gif$ - [NC,S=2]
RewriteRule .ico$ - [NC,S=1]
# none of the above extensions:
RewriteRule .*$ admin/urlHandler.php [L] |
Files with any of a list of extensions should redirect to urlHandler , but decent syntax is not available until Apache 2.4. The full extension list is php , html , css , js , txt , xml , jar , jnlp , ico , bmp , jpg , gif , png . |
RewriteCond %{DOCUMENT_ROOT}/$1 -f
RewriteRule ^(.*)$ - [L] |
If the requested file exists, go ahead and display it. |
RewriteCond %{DOCUMENT_ROOT}/$1/\
localcaptions.cap -f RewriteCond $2 !=index RewriteCond $2 !=home RewriteRule ^(.*)/([^/]+)\.([^./]+)$ \
library/servepicture.php\
?cap=/$1/localcaptions.cap\
&image=$2.$3 [L] |
Use servepicture for files in directories containing localcaptions.cap . |
RewriteRule ^.*$ admin/urlHandler.php [L] |
Fail all else, hand off to urlHandler . |