If they don??™t match, we do a 301 redirect to the proper version of the URL.
CHAPTER 7 ?– SEARCH ENGINE OPTIMIZATION 203
As pointed out earlier, URL correction is useful when somebody types a URL with a typo,
such as http://localhost/tshirtshop/natureTYPO-d2/, or when you change the name of
a product, category, or department, which causes URL changes as well.
Exercise: Implementing URL Correction
1. URL correction and other features we implement in this chapter involve working with the HTTP headers. To
avoid any problems setting the headers, we need to make this change in index.php. Add the following
highlighted code to your index.php file:
// Activate session
session_start();
// Start output buffer
ob_start();
// Include utility files
require_once 'include/config.php';
require_once BUSINESS_DIR . 'error_handler.php';
2. At the end of index.php, add the following code:
// Close database connection
DatabaseHandler::Close();
// Output content from the buffer
flush();
ob_flush();
ob_end_clean();
?>
3. Add the CheckRequest() method to the Link class in the presentation/link.php file:
// Redirects to proper URL if not already there
public static function CheckRequest()
{
$proper_url = '';
// Obtain proper URL for category pages
if (isset ($_GET['DepartmentId']) && isset ($_GET['CategoryId']))
{
if (isset ($_GET['Page']))
$proper_url = self::ToCategory($_GET['DepartmentId'],
$_GET['CategoryId'], $_GET['Page']);
else
$proper_url = self::ToCategory($_GET['DepartmentId'],
$_GET['CategoryId']);
}
CHAPTER 7 ?– SEARCH ENGINE OPTIMIZATION 204
// Obtain proper URL for department pages
elseif (isset ($_GET['DepartmentId']))
{
if (isset ($_GET['Page']))
$proper_url = self::ToDepartment($_GET['DepartmentId'],
$_GET['Page']);
else
$proper_url = self::ToDepartment($_GET['DepartmentId']);
}
// Obtain proper URL for product pages
elseif (isset ($_GET['ProductId']))
{
$proper_url = self::ToProduct($_GET['ProductId']);
}
// Obtain proper URL for the home page
else
{
if (isset($_GET['Page']))
$proper_url = self::ToIndex($_GET['Page']);
else
$proper_url = self::ToIndex();
}
/* Remove the virtual location from the requested URL
so we can compare paths */
$requested_url = self::Build(str_replace(VIRTUAL_LOCATION, '',
$_SERVER['REQUEST_URI']));
// 301 redirect to the proper URL if necessary
if ($requested_url != $proper_url)
{
// Clean output buffer
ob_clean();
// Redirect 301
header('HTTP/1.
Pages:
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316