Rewriting Keyword-Rich URLs
Here’s where the real fun begins! This kind of URL rewriting is a bit more complex, and there are more
strategies you could take. When working with rewritten numeric URLs, it was relatively easy to extract
the product and category IDs from a URL such as
, and rewrite the URL to
A keyword-rich URL doesn’t necessarily have to include any IDs. Take a look at this one:
(You met a similar example in the first exercise of this chapter, where you handled the rewriting of
This URL refers to a product named “Super Drill” located in a category named “Tools.” Obviously, if
you want to support this kind of URL, you need some kind of mechanism to find the IDs of the cate-
gory and product the URL refers to.
One solution that comes to mind is to add a column in the product information table that associates such
beautified URLs to “real” URLs that your application can handle. In such a request you could look up
the information in the Category and Product tables, get their IDs, and use them.
However, we propose an easier solution that is more easily implemented and still brings the benefits of a
keyword-rich URL. Look at the following URLs:
These URLs include keywords. However, we’ve sneaked IDs in these URLs, in a way that isn’t unpleasant
to the human eye, and doesn’t distract attention from the keywords that matter, either.
In the case of the first URL, the rewriting rule can simply extract the number that is tied at the end of the
product name (
), and ignore the rest of the URL. For the second URL, the rewriting rule can extract
the category ID (
) and product ID (
), and then use these numbers to build a URL such as
The rewrite rule for keyword-rich URLs with a single parameter looks like this:
RewriteRule ^Products/.*-P([0-9]+)\.html?$ /product.php?product_id=$1 [L]
The rewrite rule for keyword-rich URLs with two parameters looks like this:
Take a look at this latter rule at work in an exercise.
As you will see in Chapter 14 in the e-commerce demo, you will use one-parameter URLs to imple-
ment the category pages (which contain lists of products), and two-parameter URLs to implement
Chapter 3: Provocative SE-Friendly URLs
c03.qxd:c03 10:39 64