[ Pobierz całość w formacie PDF ]
process should be started all over again from the beginning. The next run through the
rewriting process is done with the rewritten URL, not the originally requested URL.
This technique is typically used in order to do a modification that appears multiple
times in a URL. Thus, it is run through the rewrite process several times to ensure that
all occurrences were caught.
5610_c04_final.qxd 1/10/06 1:13 AM Page 42
42 CHAPTER 4 % THE REWRITE RULE DIRECTIVE
The danger with this technique is that it is very easy to get caught in an infinite loop
when using the [N]flag. In many cases, the RewriteMapdirective might be a better way to
handle these scenarios.
However, to offer one simple example, consider this rule, which replaces -(dash)
with _(underscore) throughout a URI:
RewriteRule (.*)-(.*) $1_$2 [N]
This rule will run repeatedly until there are no more dashes in the requested URI. For
example, if a requested URI looks like /x--y-z, the rule will be run three times once for
each dash in the URI and then exit when there are no more dashes.
No Case: NC
Adding the No Case, or [NC], flag to any RewriteRulemakes that rule case insensitive.
That is, a rule with this flag will not care whether the text that it is considering is upper-
case or lowercase. The default behavior of the RewriteRuledirective is to be case
sensitive.
%Caution Case-insensitive pattern matching will be slightly slower.
RewriteRule ^/article/(\d*) /var/www/index.php?articleID=$1 \
[NC,H=application/x-httpd-php]
This example will look for URLs that look like /article/12or perhaps like
/Article/173and rewrite those URLs to be an argument to another URL in this
case, a PHP file.
No Escape: NE
If the target URL of a RewriteRulecontains special characters (generally speaking, any
nonalphanumeric characters), those characters will be converted into their hexcode
equivalents. For example, if a target URL contains a percentage sign (%), that character
will instead be converted to its equivalent representation, %25.
There are, of course, times when this is not desirable, and you do in fact want the
literal character that you have specified to appear in the rewritten URL. One common
example of this is the #character, which, in HTML, allows you to jump to a particular
named section of a document. By default, if a #character appears in a rewrite target, it
5610_c04_final.qxd 1/10/06 1:13 AM Page 43
CHAPTER 4 % THE REWRITE RULE DIRECTIVE 43
will be converted to %23, which will cause the rewritten URL to fail. By using the [NE]flag,
this can be avoided:
RewriteRule ^/docs/(.*) /usr/docs/directives.html#$1 [NE]
This rule takes a request for a particular configuration directive and maps that to
the exact location within a larger document listing all of the directives. And the [NE]flag
ensures that the #is actually treated as an anchor.
No Subrequest: NS
The No Subrequest, or [NS], flag is extremely useful when one file includes another file
via a subrequest. For example, when an HTML file includes other HTML files via the
Server-Side Include (SSI) mechanism, the included files are requested from Apache via
a subrequest. Generally, you don t want your RewriteRules applying to these subrequests,
since that would cause the request paths to be broken. The [NS]flag specifies that the
rule should not be applied to requests if they are subrequests.
Images and CSS files embedded in an HTML page are not retrieved via subrequests.
RewriteRule ^/ssi/(.*) /includes/files/$1 [NS]
Proxy: P
Using the Proxy, or [P], flag forces the rewritten URL to be fetched from another server
via the proxy mechanism provided by mod_proxy. You must have mod_proxy installed
in order to use the [P]flag. The rewrite target argument must be a fully qualified URL.
We will return to the topic of proxying in Chapter 11, and so just one example is
offered here. In this example, we wish to have all images served from a dedicated image
server:
RewriteRule ^/images/(.*) http://images.example.com/$1 [P]
ProxyPassReverse /images http://images.example.com
Further explanation of this example will be left until Chapter 11.
Passthru: PT
As mentioned earlier, by default, a rewrite target not starting with http://or other proto-
[ Pobierz całość w formacie PDF ]