usermike |
02-11-2003 09:08 AM |
Custom 404 Error Search Page
This script is based on the search.php file in the /customer directory.
search404.php is a custom 404 error page that no site should be without. It is designed to parce the url and take the keywords that it finds and create a custom results page for your customers.
For instance, say your old site had the page, www.yoursite.com/computer/printer/hp.htm. This script will take the url and create the following keywords: computer, printer, and hp. Then it will perform a custom search and the 404 error page people will see is the search results for these keywords.
So, URL's that didn't exist before now exist with custom tailored results. If it finds no results, it will redirect the users to your main page (or whatever you specify).
This is very good if you had old placements in search engines that you don't want to go to waste.
Paste this code into a file and place it next to search.php in the /customer directory.
Code:
<?
#
# $Id: search404.php,v 1.35 2003/2/11 12:00:00 mweiss Exp $
#
require "../smarty.php";
require "../config.php";
@include "./https.php";
require "./auth.php";
require "../include/categories.php";
//add pieces or uri to array
$pieces = explode('/', strtolower($REQUEST_URI));
// eliminate doubles
$pieces = array_unique($pieces);
// Depending on the structure your old site had you might want
// to change the value of $i. For instance, in
// www.yoursite.com/customer/chairs/bar/stools.html
// $pieces[0] will hold 'customer'
// so $i should be 1.
$i = 1;
// page to redirect page if no results are found
$redir_page = "/customer/home.php";
$oldkeywords_prod = '';
$oldkeywords_desc = '';
$length = count($pieces);
while ($i < $length) {
// strings you would like to replace
// here, I take out the extensions of pages that people
// might enter from my old website
$dummy = $pieces[$i];
$patterns[0] = '/.htm/';
$patterns[1] = '/.html/';
$patterns[2] = '/.php/';
$patterns[3] = '/.asp/';
// replace the abov extensions with nothing
$replacements[0] = '';
$replacements[1] = '';
$replacements[2] = '';
$replacements[3] = '';
// replace all the instances of 'patterns' with 'replacements' in dummy.
$pieces[$i] = preg_replace($patterns, $replacements, $dummy);
if($i == ($length-1))
{
$oldkeywords_prod .= $pieces[$i];
$oldkeywords_desc .= $pieces[$i];
}
else
{
//build our product query
$oldkeywords_prod .= $pieces[$i] . "%' OR $sql_tbl[products].product like '%";
//build our description query
$oldkeywords_desc .= $pieces[$i] . "%' OR $sql_tbl[products].descr like '%";
}
$i++;
}
//begin code from search.php
$price_condition = $price_search_1?" AND $sql_tbl[pricing].price>='$price_search_1'":"";
$price_condition .= $price_search_2?" AND $sql_tbl[pricing].price<='$price_search_2'":"";
$sort_by_price = $price_condition?" ORDER BY price":"";
$price_substring = $price_search_1?"&price_search_1=".urlencode($price_search_1):"";
$price_substring .= $price_search_2?"&price_search_2=".urlencode($price_search_2):"";
$search_category = addslashes(array_pop(func_query_first("select category from $sql_tbl[categories] where categoryid='$in_category'")));
//substitute in our new strings for the query
$search_query = "($sql_tbl[products].product like '%$oldkeywords_prod%' or $sql_tbl[products].descr like '%$oldkeywords_desc%') and $sql_tbl[categories].category like '$search_category%' and $sql_tbl[products].forsale='Y' $price_condition ".$sort_by_price;
if ($current_area == "C") {
$membership_condition = " AND ($sql_tbl[categories].membership='". $user_account['membership']."' OR $sql_tbl[categories].membership='') ";
} else {
$membership_condition = "";
}
//substitute in our new strings for the query
$search_query_count = "select count(*) from $sql_tbl[products], $sql_tbl[pricing], $sql_tbl[categories] where $sql_tbl[pricing].productid=$sql_tbl[products].productid and $sql_tbl[pricing].quantity=1 and $sql_tbl[products].categoryid=$sql_tbl[categories].categoryid $membership_condition and ($sql_tbl[pricing].membership='". $user_account['membership']."' or $sql_tbl[pricing].membership='') and ($sql_tbl[products].product like '%$oldkeywords_prod%' or $sql_tbl[products].descr like '%$oldkeywords_desc%') and $sql_tbl[categories].category like '$search_category%' and $sql_tbl[products].forsale='Y' $price_condition ";
$total_products_in_search = array_pop(func_query_first($search_query_count));
if ($total_products_in_search < 1){
//if it doesn't find anything, redirect them to the main page
header("Location: ".$redir_page);
exit();
}
#
# Navigation code
#
$objects_per_page = $config["General"]["products_per_page"];
$total_nav_pages = ceil($total_products_in_search/$config["General"]["products_per_page"])+1;
require "../include/navigation.php";
$smarty->assign("products",func_search_products($search_query, $user_account['membership'],$first_page,$total_products_in_search));
$smarty->assign("navigation_script","search404.php?substring=".urlencode($substring)."&in_category=$in_category".$price_substring);
$HTTP_GET_VARS["substring"] = stripslashes($HTTP_GET_VARS["substring"]);
$smarty->assign("main","search");
$smarty->display("customer/home.tpl");
?>
|