Congratulations! You’ve taken your first step into the world of mod_rewrite! The example is indeed
very simplistic and without much practical value — at least compared with what you’ll learn next! But
you need to fully understand the basics of URL rewriting first.
You started by creating a very simple PHP script that takes a numeric parameter through the query
string. You could imagine this is a more involved page that displays lots of details about the product
with the ID mentioned by the
query string parameter, but in your case you’re simply
displaying a text message that confirms the ID has been correctly read from the query string. The
script is indeed very simple! Here’s its code again:
// display product details
echo ‘You have selected product #‘ . $_GET[‘product_id’];
This script is easy to understand even if you’re relatively new to PHP. The
command is used to
output text, and
tells PHP to access the value of
the query string.
For a visual example of how the predefined server variables are interpreted, see Figure 3-6.
After testing that
script works, you moved on to access this same script, but through a
URL that doesn’t physically exist on your server. This is done through URL rewriting, and you imple-
mented this by adding a rule to be processed by the mod_rewrite module.
file you’ve created in the
folder contains two lines. The first enables the URL
URL Rewriting and PHP
As Figure 3-3 describes, the PHP script is accessed
the original URL has been
rewritten. This explains why
reads the value of
version of the URL. This is helpful, because the script works fine
no matter whether you accessed
directly, or the initial request was for
another URL that was rewritten to
predefined variable, as well as almost all the other predefined PHP variables,
work with the rewritten URL. The PHP documentation pages for the predefined variables
PHP, however, also allows you retrieve the originally requested URL through
, which returns the path (excluding the domain name) of the origi-
nal request. This is helpful whenever you need to know what was the original request
initiated by the client.
Table 3-1 describes the most commonly used server variables.
Chapter 3: Provocative SE-Friendly URLs
c03.qxd:c03 10:39 52