JavaScript Editor Ajax software     Free javascripts 



Main Page

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
product_id
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
product.php
script is indeed very simple! Here’s its code again:
<?php
// 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
echo
command is used to
output text, and
$_GET[‘product_id’]
tells PHP to access the value of
product_id
contained by
the query string.
For a visual example of how the predefined server variables are interpreted, see Figure 3-6.
After testing that
product.php
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.
The
.htaccess
file you’ve created in the
phpseo
folder contains two lines. The first enables the URL
rewriting engine:
RewriteEngine On
URL Rewriting and PHP
As Figure 3-3 describes, the PHP script is accessed
after
the original URL has been
rewritten. This explains why
$_GET[‘product_id’]
reads the value of
product_id
from the
rewritten
version of the URL. This is helpful, because the script works fine
no matter whether you accessed
product.php
directly, or the initial request was for
another URL that was rewritten to
product.php
.
The
$_GET
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
are
http://www.php.net/reserved.variables
and
http://www.php.net”/
variables.predefined
.
PHP, however, also allows you retrieve the originally requested URL through
$_SERVER
[‘REQUEST_URI’]
, 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.
52
Chapter 3: Provocative SE-Friendly URLs
c03.qxd:c03 10:39 52


JavaScript Editor Ajax software     Free javascripts 
R7