• Aucun résultat trouvé

Building the Link Factory

Dans le document Search Engine Optimization with PHP (Page 92-97)

1.

In your seophpfolder, create a new folder named include.

2.

In the includefolder, create a file named config.inc.php, and add this code to it:

<?php

// site domain; no trailing ‘/‘ !

define(‘SITE_DOMAIN’, ‘http://seophp.example.com’);

?>

3.

Also in the includefolder, create a file named url_factory.inc.php, and add the _prepare_

url_text()and make_product_url()functions to it as shown in the code listing:

<?php

// include config file

require_once ‘config.inc.php’;

// prepares a string to be included in an URL function _prepare_url_text($string)

{

// remove all characters that aren’t a-z, 0-9, dash, underscore or space

$NOT_acceptable_characters_regex = ‘#[^-a-zA-Z0-9_ ]#‘;

$string = preg_replace($NOT_acceptable_characters_regex, ‘’, $string);

// remove all leading and trailing spaces

$string = trim($string);

// change all dashes, underscores and spaces to dashes

$string = preg_replace(‘#[-_ ]+#‘, ‘-‘, $string);

// return the modified string return $string;

}

If you’re a PHP OOP (object-oriented programming) fan, you might like to place these functions into a class. In this case, the _prepare_url_text()function should be a private method because it’s only intended for internal use, and the make_product_

url()would be a public method. OOP features are not used in this chapter to keep the examples simple and brief, but the underscore character is used to prefix func-tions that are meant for internal use only — such as in _prepare_url_text()— to make an eventual migration to object-oriented code easier. Later the book uses PHP OOP features when that is recommended by the specifics of the exercises.

// builds a link that contains a category and a product

function make_category_product_url($category_name, $category_id,

$product_name, $product_id) {

// prepare the product name and category name for inclusion in URL

$clean_category_name = _prepare_url_text($category_name);

$clean_product_name = _prepare_url_text($product_name);

// build the keyword-rich URL

$url = SITE_DOMAIN . ‘/Products/‘ .

$clean_category_name . ‘-C’ . $category_id . ‘/‘ .

$clean_product_name . ‘-P’ . $product_id . ‘.html’;

// return the URL return $url;

}

?>

4.

In your seophpfolder, create a file named catalog.phpwith these contents:

<?php

// load the URL factory library

require_once ‘include/url_factory.inc.php’;

?>

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.1//EN”

“http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd”>

<h1>Products on Promotion at SEO Egghead Shop</h1>

<ul>

<li>

<a href=”<?php echo make_category_product_url(“Carpenter’s Tools”, 12, i

“Belt Sander”, 45); ?>”>

Carpenter’s Tools: Belt Sander

</a>

</li>

<li>

<a href=”<?php echo make_category_product_url(“SEO Toolbox”, 6, “Link i Juice”, 31); ?>”>

SEO Toolbox: Link Juice

</a>

</li>

<li>

<a href=”<?php echo make_category_product_url(“Friends’ Shed”, 2, “AJAX i PHP Book”, 42); ?>”>

Friends’ Shed: AJAX PHP Book

</a>

</li>

</ul>

</body>

</html>

5.

You’re making use of the product.phpfile you created in the previous exercise. Here’s its code again for your reference:

<?php

// display product details

echo ‘You have selected product #‘ . $_GET[‘product_id’] .

‘ from category #‘ . $_GET[‘category_id’];

?>

6.

Add one last rule to your .htaccessfile:

RewriteEngine On

# Rewrite numeric URLs

RewriteRule ^Products/C([0-9]*)/P([0-9]*)\.html$

/product.php?category_id=$1&product_id=$2 [L]

# Rewrite keyword-rich URLs

RewriteRule ^Products/.*-C([0-9]+)/.*-P([0-9]+)\.html$

/product.php?category_id=$1&product_id=$2 [L]

# Rewrite catalog.html

RewriteRule ^catalog.html$ /catalog.php [L]

7.

Load http://seophp.example.com/catalog.htmlin your web browser. You should be shown a page like the one shown in Figure 3-14.

8.

Click one of the links, and ensure the rewriting correctly loads the product.phpscript as shown in Figure 3-15.

9.

Just for the fun of it, or if you want to refresh your memory about how the product.phpscript works, load http://seophp.example.com/product.php?category_id=6&product_id=31. You should get the same output that’s shown in Figure 3-15, but this time through a dynamic URL.

Figure 3-14

Figure 3-15

Congratulations! You’ve just implemented a very organized and powerful strategy that you can use to rewrite all the URLs in your catalog, and you’ve also implemented the URL rewriting scheme that makes them work.

Let’s analyze the code you’ve written piece by piece, starting with the _prepare_url_text()function.

This function prepares a string such as a product name or a category name to be included into a URL, by either deleting or changing to dashes those characters in a string that are invalid or not aesthetically pleasing in a URL. It retains all alphanumeric characters as-is. For example, if the function receives as a parameter the string “Friends’ Shed”, it will return “Friends-Shed.”

The _prepare_url_text()function starts by using preg_replace()to delete all characters that aren’t alphanumerical, a dash, a space, or an underscore, ensuring there are no characters left that could break your URL:

// remove all characters that aren’t a-z, 0-9, dash, underscore or space

$NOT_acceptable_characters_regex = ‘#[^-a-zA-Z0-9_ ]#‘;

$string = preg_replace($NOT_acceptable_characters_regex, ‘’, $string);

The preg_replace()PHP function allows for string manipulation using regular expressions. The origi-nal string is supplied as the third parameter. The function replaces the string portions matched by the regular expression supplied as the first parameter, with the string supplied as the second parameter.

In this case, the regular expression [^-a-zA-Z0-9_ ]matches all characters not (^) in the set of letters, numbers, dashes, underscores, or spaces. You indicate that the matching characters should be replaced with ‘’, the empty string, effectively removing them. The pound (#) character used to enclose the regu-lar expression is the delimiter character that you need to use with reguregu-lar expressions in PHP functions.

Then you continue by removing the leading and trailing spaces from the string it receives as parameter, using the PHP trim()function:

// remove all leading and trailing spaces

$string = trim($string);

Finally, you use preg_replace()once again to transform any groups of spaces, dashes, and underscores to dashes. For example, a string such as SEO___Toolbox(note there are three underscores) would be replaced to SEO-Toolbox. Then you return the transformed URL string:

// change all dashes, underscores and spaces to dashes

$string = preg_replace(‘#[-_ ]+#‘, ‘-‘, $string);

// return the modified string return $string;

Here are some examples of this transformation:

// displays Carpenters-Tools

echo _prepare_url_text(“Carpenter’s Tools”);

// displays Black-And-White

echo _prepare_url_text(“Black and White”);

_prepare_url_text()is used by make_category_product_url()to create product URLs. The make_category_product_url()function receives as parameters a category name, a product name, a category ID, and a product ID, and it uses this data to create the keyword-rich URL. The code in this function is pretty straightforward. It starts by using _prepare_url_text()to prepare the product and category names for inclusion in a URL:

// builds a link that contains a category and a product

function make_category_product_url($category_name, $category_id,

$product_name, $product_id) {

// prepare the product name and category name for inclusion in URL

$clean_category_name = _prepare_url_text($category_name);

$clean_product_name = _prepare_url_text($product_name);

Then it simply joins strings to create a string of the form ./Products/Category-Name-Cm/Product-Name/Pn.html. Finally, it returns this string:

// build the keyword-rich URL

$url = ‘./Products/‘ .

$clean_category_name . ‘-C’ . $category_id . ‘/‘ .

$clean_product_name . ‘-P’ . $product_id . ‘.html’;

// return the URL return $url;

}

Here are some examples of this function’s use, and the results:

// display /Products/Carpenters-Tools-C12/Belt-Sander-P45.html echo make_product_url(“Carpenter’s Tools”, 12, ‘Belt Sander’, 45);

// display /Products/Hammers-C3/Big-and-Mighty-Hammer-P4.html echo make_product_url(‘Hammers’, 3, ‘Big and Mighty Hammer’, 4);

The web site application should be retrofitted with make_product_url(). All instances of a web site application’s code where URLs are displayed should be replaced. For example (assume the following variables refer to product database information):

echo “/products.php?category_id=$category_id&product_id=$product_id”;

These kinds of instances should be changed to:

echo make_product_url($category_name, $category_id, $product_name, $product_id);

We’ve demonstrated how you can do this by creating a fictional catalog page of an e-commerce web site, catalog.php, and we’ve even created a rewrite rule for it so you could access it using a static URL.

In a real-world web site the product and category names and IDs would be retrieved from the database, but for the purposes of this exercise we’ve typed them by hand to simplify the example.

<a href=”<?php echo make_category_product_url(“Carpenter’s Tools”, 12, ‘Belt i Sander’, 45); ?>”>

Carpenter’s Tools: Belt Sander

</a>

Dans le document Search Engine Optimization with PHP (Page 92-97)