Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Httpd Wiki" for change 
notification.

The following page has been changed by noodl:
http://wiki.apache.org/httpd/Rewrite/Context

The comment on the change is:
First bash

New page:
== Introduction ==

The Apache HTTPD Server deals with requests in discrete phases. While this is 
usually transparent to the user and administrator it does have an effect on the 
behaviour of mod_rewrite when rulesets are placed in different contexts. To 
oversimplify a little, when rules are placed in !VirtualHost blocks (or in the 
main server context) they "run" before the server has yet mapped the requested 
URI to a filesystem path. Conversely, when rules are placed in .htaccess files, 
or in Directory blocks in the main server config, they "run" after this phase 
has occured.

This timing issue affects two areas of rewrite rule construction, the base URI 
as seen by !RewriteRule and the values of certain environment variables. See 
the note entitled "Per-directory Rewrites" 
[http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewriterule here] for 
further details.

== Base URI ==

The most visible change to look out for here is that in !VirtualHost 
(per-server) context the request URI as seen by !RewriteRule will start with a 
/ (slash). Conversely in .htaccess or Directory (per-directory) the initial 
slash is stripped.

As an example, consider this basic rule to map the request for 'foo' to 'bar'.

{{{
# in .htaccess
RewriteEngine On
RewriteRule ^foo bar
}}}

{{{
# in VirtualHost
RewriteEngine On
RewriteRule ^/foo /bar
}}}

== Environment Variables ==

As discussed, in !VirtualHost (per-server) context, rewrite rules run before 
the request has been mapped to a filesystem path. Resulting from this, certain 
of the variables available to mod_rewrite concerned with filesystem paths are 
incomplete, including SCRIPT_FILENAME (and its mirror, REQUEST_FILENAME).

As an example, the following two rulesets attempt to see if the request uri 
corresponds to an existing file and take action if not.

{{{
# in .htaccess, SCRIPT_FILENAME is complete
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule (.*) script.cgi
}}}

{{{
# in VirtualHost, SCRIPT_FILENAME is incomplete
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/%{SCRIPT_FILENAME} !-f
RewriteRule (.*) /script.cgi
}}}

In particular, in per-server context the SCRIPT_FILENAME variable will be the 
same as REQUEST_URI. (FIXME: is this correct?)

Reply via email to