Ajax software
Free javascripts
↑
Main Page
Some of these flags are used in later chapters as well.
RewriteRule
commands are processed in sequential order as they are written in the configuration file.
If you want to make sure that a rule is the last one processed in case a match is found for it, you need to
use the
[L]
flag as listed in the preceding table:
RewriteRule ^Products/P([0-9]*)\.html$ ./product.php?product_id=$1
[L]
This is particularly useful if you have a long list of
RewriteRule
commands, because using
[L]
improves
performance and prevents mod_rewrite from processing all the
RewriteRule
commands that follow once
a match is found. This is usually what you want regardless.
URL Rewriting and PHP
Regular expressions are also supported by PHP. Whenever you need to do string manipulation that is
too hard to implement using the typical PHP string functions (
http://www.php.net/strings
), the
regular expression functions can come in handy, as soon as you get accustomed to working with regular
expressions.
PHP has many regular expression functions, the most common of which are listed in Table 3-5 for your
convenience. The examples in this book use only
preg_match
and
preg_replace
, which are the most
frequently used, but it’s good to know there are others. For more information, visit
http://www.php
.net/pcre
.
Table 3-5
PHP Function
Description
preg_grep
Receives as parameters a regular expression, and an array of input
strings. The function returns an array containing the elements of the
input that match the pattern. More details at
http://www.php.net/
preg_grep
.
preg_match
Receives as parameters a regular expression and a string. If a match
is found, the function returns 1. Otherwise, it returns 0. The function
doesn’t search for more matches: after one match is found, the execu-
tion stops. More details at
http://www.php.net/preg_match
.
One detail worth keeping in mind is that the regular PHP string manipulation
functions execute much faster than regular expressions, so you should use them
only when necessary. For example, if you simply want to check whether one string
literal is included in another, using
strpos()
or
strstr()
would be much more
efficient than using
preg_match()
.
60
Chapter 3: Provocative SE-Friendly URLs
c03.qxd:c03 10:39 60
Ajax software
Free javascripts
→ R7