Author Topic: Redirecting 404 error to a search of missing page name  (Read 13950 times)

Offline fsuk

  • Posts: 127
Redirecting 404 error to a search of missing page name
« on: June 10, 2008, 03:39:51 PM »
I haven't been using WB long but i've already made a few modifications to my sites WB.

One i've made is to redirect 404 errors (page not found) to the wb search using the name of the file not found as the search term.

For this you need to have mod_rewrite enabled on your server. (google for more info)

1. Add to your .htaccess file (see here for info on .htaccess http://www.javascriptkit.com/howto/htaccess.shtml)

Code: [Select]
RewriteEngine on

#404 redirects
RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ([^/\.]+).php$ /search/index.php?string=$1&match=any&error=404 [L]

2. In search/search.php after $string = preg_replace("/(^|\s+)([-=+_&!;#]|\\\\\"|\\\\')+(?=\s+|$)/", " ", $string); on a new line add:
Code: [Select]
$string = str_replace("-", " ", $string );
3. In search/search.php after // Show search header (aprox line 154) but before echo $search_header; on a new line add:

   
Code: [Select]
   
    $error = 'none';
    if(isset($_REQUEST['error'])){
        $error = $_REQUEST['error'];
    }
        if($error == '404'){
        echo '404 Error. The page you entered was not found. <br>Please check the search results below to locate the page you are looking for.';

    }



OK what does this do? Well:
1: This checks to see if the requested page or dir exists if not then it goes to the search page using the name of the file or dir minus the .php as the search term in an any word search and adds &error=404 onto the address

2: Because the default page seperator is '-' and if you have pages named a-page.php then the it will search for 'a-page' which probably wont return anything. This replaces the '-' with a ' ' so you search for 'a page'

3: As you can see in 1 i've added a &error=404 to the address in the .htaccess file. This bit checks the address for an error code and if the error code = 404 then it adds a message to the top of the search page.

So for example: A user types the address http://www.mysite.com/blue-flamingo.php but the page dosn't exist so the user is redirected to a search page of any of the words blue flamingo. Because your site has a page named pink flamingo it shows up in the search results..

As far as i can tell it dos'nt affect the normal search or any thing else.
« Last Edit: June 10, 2008, 03:41:30 PM by fsuk »
You say deviant like its a bad thing...

Offline Argos

  • Posts: 2542
  • Gender: Male
    • Argos Media
Re: Redirecting 404 error to a search of missing page name
« Reply #1 on: August 16, 2008, 06:24:42 PM »
So for example: A user types the address http://www.mysite.com/blue-flamingo.php but the page dosn't exist so the user is redirected to a search page of any of the words blue flamingo. Because your site has a page named pink flamingo it shows up in the search results.

When would a user type the URL of a page on your site that doesn't exist? People follow links to navigate, they don't type URL's to visit another page, or do they? I don't see a real world use of your idea. It's cleverly made up though.
Jurgen Nijhuis
Argos Media
Heiloo, The Netherlands
----------------------------------------------------------------
Please don't request personal support, use the forums!

Offline fsuk

  • Posts: 127
Re: Redirecting 404 error to a search of missing page name
« Reply #2 on: August 17, 2008, 12:43:25 AM »
It would come in use for instance if the link was broken, say the page had been move, renamed or deleted.

I currently working on using something similar to this to modify wb so that it dosn't need physical pages in the pages folder by redirecting the user to a dummy page and then matching the page typed with the page link in the database to then retrive the data of the page. I should have this mod finished by the end of the week.
You say deviant like its a bad thing...

iggyFlames

  • Guest
Re: Redirecting 404 error to a search of missing page name
« Reply #3 on: August 17, 2008, 01:45:20 AM »
Thanks for share! I think it could be useful in cases when user keep the page address in "Favorites", hu?

I currently working on using something similar to this to modify wb so that it dosn't need physical pages in the pages folder by redirecting the user to a dummy page and then matching the page typed with the page link in the database to then retrive the data of the page. I should have this mod finished by the end of the week.

Oh, I'm very interested in this work. Would you tell us if you have success?

Regards,

Iggy

Offline fsuk

  • Posts: 127
Re: Redirecting 404 error to a search of missing page name
« Reply #4 on: August 17, 2008, 06:51:01 PM »
So far ive managed to get it working with the first level of menus ie not sub menus, after ive got the working i have to check that things like news posts will still work, stop wb creating the php filess and then add a settings option to turn it on an off.

I've got a lot of free time this week so it should progress quickly.
You say deviant like its a bad thing...

Offline kweitzel

  • WebsiteBaker Org e.V.
  • **
  • Posts: 6983
  • Gender: Male

Offline fsuk

  • Posts: 127
Re: Redirecting 404 error to a search of missing page name
« Reply #6 on: August 18, 2008, 01:25:14 PM »
Well it would seem that they are already planning to do this in WB3, i think ill just work on moding wb2.7
You say deviant like its a bad thing...

Offline fsuk

  • Posts: 127
Re: Redirecting 404 error to a search of missing page name
« Reply #7 on: August 18, 2008, 03:17:26 PM »
This idea has been named wbx2.7. New thread: http://www.websitebaker2.org/forum/index.php/topic,10745.0.html
You say deviant like its a bad thing...