s
[ Pobierz całość w formacie PDF ]
afresh with each request to the directory scope in question. Putting configuration direc-
tives in the main server configuration file allows for the directives to be read and parsed
a single time and the configuration tree stored in memory for rapid access.
The additional complexity comes primarily from the fact that most of the examples
in the documentation and in tutorials tend to be geared to rewrite rules that are not
placed in per-dir scope, but are in the main configuration file. This results in the need
to retrofit rules for the per-dir context. This additionally means that the same rule may
look quite different depending on which directory scope it is placed in.
Having said that, it is perfectly well understood that sometimes there is no option,
and an .htaccessfile or block must be used.
Per-Dir Examples
In the following examples, I ll attempt to be as specific as I possibly can, so that it is
abundantly clear what is going on.
First, consider the case of an .htaccessfile, which in today s web hosting environ-
ment seems to be the most common scenario for managing rewrite rules. In this example,
the web server s DocumentRootis set to /usr/local/apache/htdocsand an .htaccessfile
resides in the directory /usr/local/apache/htdocs/example/. With that in mind, consider
the examples found in Table 4-2.
Table 4-2. Correlation Between the URI and What RewriteRule Has to Work With
User Sees RewriteRule in .htaccess File Looks At
http://example.com/one Does not see this request, because it is outside
of the affected directory
http://example.com/example/index.html index.html(no leading slash)
http://example.com/example/one/two/three one/two/three(no leading slash)
http://example.com/example/index.php?one=two index.php(no leading slash, no QUERY_STRING)
http://example.com/example/index.php/one/two index.php/one/two
5610_c04_final.qxd 1/10/06 1:13 AM Page 35
CHAPTER 4 % THE REWRITE RULE DIRECTIVE 35
In the examples that follow throughout the rest of the book, where appropriate I ll try
to give versions of examples that will work in your main server configuration file, as well
as versions that will work in an .htaccessfile.1 In that way, over time, you ll hopefully
come to more clearly understand the way in which the rules differ in the per-dir context.
Finally, we ll consider the case of a section. In the hopes of keeping this
simple, you should consider RewriteRules in sections to be treated exactly
the same as having that RewriteRulein an .htaccessfile in that same directory. Which is
to say that Table 4-2 will be exactly the same if the RewriteRulewas presented like so:
RewriteRule goes here
It s actually fairly uncommon practice to place a RewriteRulewithin a
block, therefore we won t return to this scenario too much throughout the book.
The Rewrite Pattern
Next, we ll consider a commonly overlooked but important question namely, what s
the rewrite pattern for?
The way that you can think of the rewrite pattern is that it is the if clause on the
rewrite. If the URL looks like this (the rewrite pattern), then do this (the rewrite target) to
it. In other words, if the requested URL looks like the pattern, then we want to rewrite it
to the target URL. Some actual examples of this will follow in the next few pages.
The rewrite pattern, as mentioned earlier, is applied just to the local URI that is,
to what the user sees in their browser address bar, minus the http://part and the
hostname.
Rewrite Target
The rewrite target the second argument of the RewriteRuledirective determines
where you want the requested URL to go instead.
%Note In the Apache documentation, the target is frequently called the substitution.
1. I tend to learn better by examples, and I ll make the assumption that you do, too. Most of my technical
support experience is via IRC, and this assumption seems to hold up pretty well there.
5610_c04_final.qxd 1/10/06 1:13 AM Page 36
36 CHAPTER 4 % THE REWRITE RULE DIRECTIVE
In its simplest incarnations, this is just a redirect: if the URL looks like this, send it
here instead. But, as you ll soon see, it quickly gets a lot more complicated than that. For
our first example, however, we ll start with something that simple:
RewriteEngine On
[ Pobierz całość w formacie PDF ]