mod-rewrite Contexts of rewrite rules Rewrite rules in per-directory context


Example

The per-directory context is a part of the static configuration file between <Directory> and </Directory> tags. The entire content of dynamic configuration files is within the per-directory context of the folder in which the .htaccess resides.

RewriteRule's in per-directory context match against the part of an url after the protocol, hostname, port and prefix of the directory in which they reside, and before the query string.

In the static configuration file

When the following rule is used on the url http://example.com/foo?id=1, the regex in the first argument of RewriteRule is matched against foo. The protocol (http), hostname (example.com) and prefix for this directory (/) are removed. At the other end, the query string (?id=1) is also removed.

<Directory "/">
  RewriteRule ^foo$ bar [L]
</Directory>

In the following example, using the url http://example.com/topic/15-my-topic-name, the first argument of RewriteRule would be matched against topic/15-my-topic-name:

<Directory "/topic/">
  RewriteRule ^topic/([0-9]+)-[^/]*/?$ topics.php?id=$1 [L]
</Directory>

In the dynamic configuration file

When the following rule is placed in a .htaccess file that is in the www-root folder and then used on the url http://example.com/foo?id=1, the first regex is matched against foo.

RewriteRule ^foo$ bar [L]

In per-directory context, the matched url never starts with a /. In such a context, a directive starting with RewriteRule ^/ will never match anything.