Home > osCommerce > Shiny Red Apples

Shiny Red Apples

I’ve mentioned in an earlier blogging entry about the possibilities the new framework brings in regards to SEFU (Search Engine Friendly URLs) and SEO (Search Engine Optimization) by providing the following example URLs:

/products.php/45
/products/45
/products/product_keyword_here

The most common method to retrieve a product from the database is to place the product ID in the URL. This can be seen in the above example with the number 45 existing in the URL.

Although that is a quick and easy way to retrieve a product, it leaves the URL messy providing absolutely no information to the customer nor to the search engines indexing the page.

This is now a thing of the past for osCommerce where a keyword is instead used to retrieve the product from the database. As with the product ID, this keyword needs to be unique in the database so the right product can be retrieved, with the additional bonus of being able to use translated keywords to find the same product in different languages.

For a store selling shiny red apples, this means that the urls pointing to the product information page can use the style of:

/products/shiny_red_apples

and for their German product information page:

/products/lustvolle_rote_aepfeln

If there is no more products_id=45 in the URL, how does the new framework know when to retrieve a specific product from the database? The product information page, for example, knows it is expecting a value to retrieve a product from the database, and checks the first parameter in the URL to see if it is a valid product identifier or not.

This is being done with the following code:

foreach ($_GET as $key => $value) {
if (is_numeric($key) || ereg('[0-9]+[{[0-9]+}[0-9]+]*$', $key) || ereg('[a-zA-Z0-9-_]*$', $key)) {
$id = $key;
}

break;
}

if (osC_Product::checkEntry($id)) {
$osC_Product = new osC_Product($id);
}

This checks to see if the first parameter is numeric (as usual), is numeric and contains attribute parameters, and if it is a word containing only a-zA-Z0-9-_ characters. If this check passes, it calls a method in the osC_Product class to check if the product exists in the database and if it’s status is activated.

The osC_Product class is able to differentiate between the 3 types of parameters it is passed (numeric, numeric + attribute pairs, keyword), and creates and instance of the class containing the relevant product information.

Checking to see if a product is to be retrieved has so far been done as:

if (isset($_GET['products_id']) && is_numeric($_GET['products_id'])) {
...
}

As this is no longer the case for the new framework, and instead of looping through the URL each time to see if a product is to be retrieved, it is now logical that if the osC_Product class has been instantiated, that a product is being requested and that a valid product in the database has been retrieved. This can then be checked on other areas of the page with:

if (isset($osC_Product) && is_a($osC_Product, 'osC_Product)) {
...
}

The is_a check is performed to make sure the $osC_Product variable is indeed an instance of the osC_Product class.

It is a tiny bit rough and will see if returning an object (regardless) with the static checkEntry() method is an easier approach to take. (ie, $osC_Product->isValid())

Retrieving the product name is then as easy as doing:

echo $osC_Product->getTitle();

And instead of products being linked to through their ID, they are now linked to with their defined keyword. This provides a nice clean URL, hides the internal product ID from the public, and optimizes the page for search engine indexing.

There is always a catch though when wanting to keep the URL clean – as the first parameter is used in this case as a product identifier, the first parameter in the URL is also used to call a page module, for example:

/products/specials
/products/new
/products/reviews

This means a product keyword cannot be the same as a page module name, otherwise the page module will load instead of the product information page. :oops: So much for striving for perfection ;-)

Categories: osCommerce Tags:
  1. No comments yet.
  1. No trackbacks yet.
You must be logged in to post a comment.