X-Cart: shopping cart software

X-Cart forums (https://forum.x-cart.com/index.php)
-   Dev Questions (https://forum.x-cart.com/forumdisplay.php?f=20)
-   -   Custom 404 Error Search Page (https://forum.x-cart.com/showthread.php?t=1471)

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");
?>


jpsowin 02-11-2003 09:23 AM

Wow, great idea! I'll have to try it...

funkydunk 02-12-2003 03:42 AM

Nice script 8)

I tend to use a different error reporting script but it essentially does the same thing.

ambermoon 03-25-2003 12:19 AM

not working
 
for some reason this script is not working with me

any sugestions ?

need300z 06-18-2003 07:53 AM

Ok i created a file called bettersearch.php pasted the above code into it and ftp'd the file into the same directory as search.php but for some reason the new file doesn't work? how do i get it to work.

RazorWriter 06-28-2003 11:56 AM

I'm having the same problem with this script. How do we get it to work? I don't understand how the search script knows about this file. Help?

adpboss 06-28-2003 05:01 PM

Someone please help.

I would love to be able to use this script as well.

RazorWriter 07-25-2003 09:34 AM

Funkydunk,
would you be willing to share the script that you are using to acheive this effect?

*We cannot get this one to work* and it's very important (for me) that our x-cart store capture all the traffic that is being achieved by our old site's Google rankings. I'd be willing to pay if you were cool with it.

adpboss 11-27-2003 03:49 PM

Anyone got one of these?

I will kick in some cash. It is needed now more than ever....

DogByteMan 07-31-2004 07:57 PM

adpboss.... did you, or anyone who reads this, get this to work. If so, how?

adpboss 07-31-2004 09:04 PM

http://www.adpmods.com/dogbyteman.html

NOPE. I just made a pretty 404 error page. This would have been a cool mod, but I have moved way beyond it.

DogByteMan 07-31-2004 09:12 PM

I can make it work somewhat redirecting with .htaccess containing /xcart/customer/search404.php but the page shows without pictures and formatting. If I use http://yourdomain.com/xcart/customer/search404.php as the redirect the formatting is good but the search is bad.

DogByteMan 08-01-2004 03:15 PM

I've been trying to understand why if you use the following placed in a .htaccess file, the resulting page is without pictures, template formatting and CSS.

ErrorDocument 404 /xcart/customer/search404.php

However if you use...

ErrorDocument 404 http://www.yourdomain.com/xcart/customer/search404.php

Everything shows perfectly.

WHY?

DogByteMan 08-03-2004 02:31 PM

If I could just get an answer to the above post, I might be able to make this script work...

adpboss 08-03-2004 02:38 PM

Wish I could help buddy, but this is out of my league.

DogByteMan 08-04-2004 01:53 PM

I'm Sooooooo Close..... Yet Soooooooo Far

TonyD 09-08-2004 09:56 AM

i know i am Waaaay late on this...but i got it to work by simply going into my control panel and adding a shtml redirect...

so that anytime someone goes to a page that doesnt exist, they get redirected to this new search404 and it seems to do the trick for me....
if you want to use this you will need to adjust the name/location of your search404.php file....

Code:

<HTML>
<head>
<meta HTTP-EQUIV="Refresh" CONTENT="1; URL=search404.php">
</head>
</HTML>

<!--
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
-->


~TonyD

DogByteMan 11-21-2004 12:09 PM

Anybody ever upgraded this code to work with 4.0.x?

markwhoo 01-05-2005 06:57 PM

I am not sure if this will help you guys out or not.

I have done this on several sites and it seems to help. This helps to not loose to many customers and keeps them in the store.

I recieved this from a friend of mine and not sure where it came from, but I know I can't take the credit for making it. Just sharing it. Hope no one minds a sizable post cause it is a bit lengthy. here goes enjoy...

Code:


Error messages have numerous causes, such as misspellings, outdated links or internal server errors. When an error is encountered, your server will display specific generic error pages according to the error. These error pages are not only dead ends, but they are also very frustrating for your potential visitors.

When your visitors mistype your web address or click on an outdated link and receive the dreaded error page, they'll most-likely click on their back button and never return. However, you can recover a majority of your lost visitors simply by taking the time to create some customized, user friendly error pages.

As servers run different types of software and do not function in the same manner, there isn't a simple method for creating custom error pages that will work with every system. However, if you have your own domain and your site is hosted on a Unix/Linux server running Apache, this article will assist you in creating custom error pages.

If you're not sure what type of server you're on, visit the following web address to find out: http://uptime.netcraft.com/up/graph/
Before we begin, keep in mind, editing your server files is serious business. Even one small typographical error can wreak havoc -- make sure you make a backup copy of any file you're planning to edit.

Guidelines for creating your error pages:

1. Create your error pages in standard HTML -- just as you would create any other web page for your site.

2. Don't alarm your visitors. Never include the word "ERROR" in large, bold text. Your visitors may immediately become alarmed and think they've done something to cause the error. Instead, be apologetic and encourage your visitors to click on the navigational links to locate additional resources and information.

3. Your error pages should look just like the rest of your web pages. Each error page should contain good navigational links, a search feature, and provide information in regard to the specific error they received.

Once you've created an error page, save it as the error name. For example, if you're creating a customized error page for a 400 Bad Request error, your page should be saved as 400.html.

Here are some of the more common errors:

400 Bad Request
401 Authorization Required
403 Forbidden
404 File Not Found
405 Method Not Allowed
500 Internal Server Error
501 Method Not Implemented
502 Bad Gateway
503 Service Temporarily Unavailable

Once you've created your pages, you'll need to access your server via FTP and create a new folder called "errordocs" where you store your HTML files. Upload your new error documents into your new folder.

Your next step will be to locate your .htaccess file and download it to your computer. (If you use FrontPage to publish your web pages, you cannot customize the .htaccess file, as FrontPage uses the .htaccess file. Editing the file may cause errors in your configuration.) The .htaccess file should be located on your server where you store your HTML files.

If the .htaccess file isn't visible, you can create one within a plain text editor. However, you must first make sure your server isn't configured to hide the file. Your FTP program should enable you to choose to display hidden files and folders on your server.

Once you've downloaded your .htaccess file, open it within a plain text editor, such as Note Pad, and add the following lines below any other text that may be present:

ErrorDocument 400 /errordocs/400.html
ErrorDocument 401 /errordocs/401.html
ErrorDocument 403 /errordocs/403.html
ErrorDocument 404 /errordocs/404.html
ErrorDocument 405 /errordocs/405.html
ErrorDocument 500 /errordocs/500.html
ErrorDocument 501 /errordocs/501.html
ErrorDocument 502 /errordocs/502.html
ErrorDocument 503 /errordocs/503.html

If you're creating your own .htaccess file, open a plain text editor and add the above lines.

When typing in the information, make certain you type it exactly as it appears above. You can include the error documents of your choice.
Once the file is complete, save it as .htaccess and upload it to your server, via FTP in ASCII mode, where you store your HTML files.

If you have a Windows operating system, you will be unable to save the file as .htaccess. You'll need to save it as htaccess.txt. Once you upload the file to your server, you can rename it to .htaccess.

That's all there is to it. When your visitors click on an outdated link, your custom error page will now be displayed.

Creating your own custom error pages is well worth the time and effort, as they will enable you to recover an unlimited number of your visitors. If you follow this step by step guide, you can have your pages up and running in no time.


Well, there ya go. I am sure a lot of you already knew this, but for those who may not, now you do. 8)

markwhoo 01-24-2005 04:13 PM

I know this is an old post, but I thoght I would add a tptal list of error pages for those that wish to use this capability in the .htaccess referral I posted above.
  • Client Error 4xx ... 10.4
    400 Bad Request ... 10.4.1
    401 Unauthorized ... 10.4.2
    402 Payment Required ... 10.4.3
    403 Forbidden ... 10.4.4
    404 Not Found ... 10.4.5
    405 Method Not Allowed ... 10.4.6
    406 Not Acceptable ... 10.4.7
    407 Proxy Authentication Required ... 10.4.8
    408 Request Timeout ... 10.4.9
    409 Conflict ... 10.4.10
    410 Gone ... 10.4.11
    411 Length Required ... 10.4.12
    412 Precondition Failed ... 10.4.13
    413 Request Entity Too Large ... 10.4.14
    414 Request-URI Too Long ... 10.4.15
    415 Unsupported Media Type ... 10.4.16
    416 Requested Range Not Satisfiable ... 10.4.17
    417 Expectation Failed ... 10.4.18
    Server Error 5xx ... 10.5
    500 Internal Server Error ... 10.5.1
    501 Not Implemented ... 10.5.2
    502 Bad Gateway ... 10.5.3
    503 Service Unavailable ... 10.5.4
    504 Gateway Timeout ... 10.5.5
    505 HTTP Version Not Supported ... 10.5.6


All times are GMT -8. The time now is 07:30 AM.

Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.