JavaScript Editor Ajax software     Free javascripts 



Main Page

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
/Products/C5/P9.html
, and rewrite the URL to
product.php?category_id=5&product_id=9
.
A keyword-rich URL doesn’t necessarily have to include any IDs. Take a look at this one:
http://www.example.com/Products/Tools/Super-Drill.html
(You met a similar example in the first exercise of this chapter, where you handled the rewriting of
http://seophp.example.com/my-super-product.html
.)
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:
http://www.products.com/Products/Super-Drill-P9.html
http://www.products.com/Products/Tools-C5/Super-Drill-P9.html
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 (
-P9
), and ignore the rest of the URL. For the second URL, the rewriting rule can extract
the category ID (
-C5
) and product ID (
-P9
), and then use these numbers to build a URL such as
product
.php?category_id=5&product_id=9
.
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:
RewriteRule ^Products/.*-C([0-9]+)/.*-P([0-9]+)\.html$
i
/product.php?category_id=$1&product_id=$2 [L]
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
64
Chapter 3: Provocative SE-Friendly URLs
c03.qxd:c03 10:39 64


JavaScript Editor Ajax software     Free javascripts 
Bitcoin Gambling Site