Perhaps the most frequently asked rewrite question seen on #apache involves mapping arbitrary URIs to a handler script or proxy URL. There are several variations on this theme, detailed below.

The technique of mapping all but a few URI patterns to a handler can be used to delegate the process of URI translation to a more capable programming environment than is provided by mod_rewrite. It is easier to parse /books/history/french_revolution with a scripting language than with a set of depth-limited patterns, for example.

The rules shown here are designed to work in VirtualHost context. For information on translating them for .htaccess context, see RewriteContext

Map all URIs except those corresponding to existing files to a handler

RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-d
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
RewriteRule . /script.php

This ruleset and the following will usually require the script to interpret the REQUEST_URI environment variable to determine what actions to take.

Map any request to a handler

In the case where all URIs should be sent to the same place (including potentially requests for static content) the method to use depends on the type of the handler. For php scripts, use:

For other handlers such as php scripts, use:

RewriteEngine On
RewriteCond %{REQUEST_URI} !=/script.php
RewriteRule .* /script.php

And for CGI scripts:

ScriptAliasMatch .* /var/www/script.cgi

Map URIs corresponding to existing files to a handler instead

RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f
RewriteCond %{REQUEST_URI} !=/script.php
RewriteRule .* /script.php

If the existing files you wish to have handled by your script have a common set of file extensions distinct from that of the hander, you can bypass mod_rewrite and use instead mod_actions. Let's say you want all .html and .tpl files to be dealt with by your script:

Action foo-action /script.php
AddHandler foo-action html tpl