Ajax software
Free javascripts
↑
Main Page
Figure 3-13
Building a Link Factory
Back in the days when you worked only with dynamic URLs, it was easy to build the URLs right in
the application code without much planning. You cannot do that here. If you want to use keyword-rich
URLs in your web site, just having a
RewriteRule
in your
.htaccess
file isn’t enough! You also need
to ensure that all the links in your web site use these keyword-rich versions consistently throughout the
web site. Obviously, including the URLs manually in the web site is not an option — it’s a dynamic site
after all, and the link management task would quickly become impossible to handle when you have a
large number of products in your catalog!
Fortunately, there’s a very straightforward solution to this problem, which as soon as it’s put in place,
takes away any additional link management effort. The solution we’re proposing is to use a function to
generate the new URLs based on data already existing in your database, such as product or category
names. As mentioned before, this also enforces consistency.
Say that you have a product called Super Drill, located in the category Tools. You know the product ID
is 9, and the category ID is 5. It’s quite easy to create a PHP function that uses this data to generate a link
such as
/Products/Tools-C5/Super-Drill-P9.html
.
In the exercise that follows you create and then use two PHP functions:
?
_prepare_url_text
receives as a parameter a string that will be included into the beautified
URL, such as a product or category name, and transforms it into a form that can be included in
a URL. For example, this function would transform Super Drill to Super-Drill.
?
make_product_url
takes as parameters the names of a product and a category, and their IDs,
and uses the
_prepare_url_text
function to generate a URL such as
/Products/Tools-C5/
Super-Drill/P9.html
.
^Products/.*–C([0–9]+)/.*–P([0–9]+)\.html$
/product.php?category_id=$1&product_id=$2
/product.php?category_id=5&product_id=99
mod_rewrite
translates request
PHP script executes
Client requests URL
http://seophp.example.com/Products/Tools-C5/Super-Drill-P99.html
66
Chapter 3: Provocative SE-Friendly URLs
c03.qxd:c03 10:39 66
Ajax software
Free javascripts
→ R7