JavaScript Editor Ajax software     Free javascripts 



Main Page

As you learned in the previous exercise, a basic
RewriteRule
takes two arguments. In this example it also
received a special flag —
[L]
— as a third argument. The meaning of these arguments is discussed next.
The first argument of
RewriteRule
is a regular expression that describes the
matching
URLs you
want to rewrite. The second argument specifies the destination (
rewritten
) URL. So, in geek-speak, the
RewriteRule
line basically says: rewrite any URL that
matches
the
^Products/P([0-9]*)\.html$
pattern to
/product.php?product_id=$1
. In English, the same line can be roughly read as: “dele-
gate any request to a URL that looks like
Products/P
n
.html
to
/product.php?product_id=
n
”.
Let’s analyze in detail the regular expression that describes the matching URLs that need to be rewritten.
That regular expression is:
^Products/P([0-9]*)\.html$
Most characters, including alphanumeric characters, are read literally and simply match themselves.
Remember the first
RewriteRule
you’ve written in this chapter to match
my-super-product.html
,
which was mostly created of such “normal” characters.
What makes regular expressions so powerful (and sometimes complicated), are the special characters (or
metacharacters
), such as
^
,
.
, or
*
, which have special meanings. Table 3-3 describes the metacharacters
you’ll meet.
Remember that this theory applies to regular expressions in general. URL rewriting is just one of the
many areas where regular expressions are used.
Table 3-3
Table continued on following page
Metacharacter Description
^
Matches the beginning of the line. In our case, it will always match the beginning of
the URL. The domain name isn’t considered part of the URL, as far
RewriteRule
is
concerned. It is useful to think of
^
as “anchoring” the characters that follow to the
beginning of the string, that is, asserting that they be the first part.
.
Matches any single character.
*
Specifies that the preceding character or expression can be repeated zero or more
times — not at all to infinite.
+
Specifies that the preceding character or expression can be repeated one or more
times. In other words, the preceding character or expression must match at least
once.
?
Specifies that the preceding character or expression can be repeated zero or one
time. In other words, the preceding character or expression is optional.
{m,n}
Specifies that the preceding character or expression can be repeated between
m
and
n
times;
m
and
n
are integers, and
m
needs to be lower than
n
.
57
Chapter 3: Provocative SE-Friendly URLs
c03.qxd:c03 10:39 57


JavaScript Editor Ajax software     Free javascripts 
Bitcoin Gambling - The Original Crypto Dice Game