Welcome, Guest. Please login or register.
Did you miss your activation email?
February 10, 2012, 01:40:32 AM

Login with username, password and session length
Search:     Advanced search
Interested in joining the WebsiteBaker team?
For more Information read here or on our new website.
149483 Posts in 21084 Topics by 7530 Members
Latest Member: hoschmeck
* Home Help Search Login Register
Pages: [1]   Go Down
Print
Author Topic: Redirecting 404 error to a search of missing page name  (Read 5884 times)
fsuk

Offline Offline

Posts: 127



« on: June 10, 2008, 02: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:
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:
$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:
   
    $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, 02:41:30 PM by fsuk » Logged

You say deviant like its a bad thing...
Argos
Moderator
**
Offline Offline

Posts: 2084


WWW
« Reply #1 on: August 16, 2008, 05: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.
Logged

Jurgen Nijhuis
Argos Media
Heiloo, The Netherlands
WB Showcase: http://www.mywebsitebaker.com/pages/showcase.php?v&category_id=1242&count=30
----------------------------------------------------------------
Please don't request personal support, use the forums!
fsuk

Offline Offline

Posts: 127



« Reply #2 on: August 16, 2008, 11:43:25 PM »

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.
Logged

You say deviant like its a bad thing...
iggyFlames

Offline Offline

Posts: 25



« Reply #3 on: August 17, 2008, 12: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
Logged
fsuk

Offline Offline

Posts: 127



« Reply #4 on: August 17, 2008, 05: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.
Logged

You say deviant like its a bad thing...
kweitzel
Forum administrator
*****
Offline Offline

Posts: 6817


WWW
« Reply #5 on: August 17, 2008, 07:16:03 PM »

Have a look here as well: http://help.websitebaker.org/pages/en/knowledge-base/custom-error-pages.php

cheers

Klaus
Logged

WebsiteBaker Org e.V. - for WebsiteBaker

fsuk

Offline Offline

Posts: 127



« Reply #6 on: August 18, 2008, 12: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
Logged

You say deviant like its a bad thing...
fsuk

Offline Offline

Posts: 127



« Reply #7 on: August 18, 2008, 02:17:26 PM »

This idea has been named wbx2.7. New thread: http://www.websitebaker2.org/forum/index.php/topic,10745.0.html
Logged

You say deviant like its a bad thing...
ClareYeomans
Guest
« Reply #8 on: March 11, 2011, 04:15:11 PM »

hey,

I was having the same issue and and i tried may solution but it didn't work out.
Although your code helped me allot but getting the error 404 errors (page not found).

senuke x | p90x
« Last Edit: April 06, 2011, 08:37:25 PM by ClareYeomans » Logged
Pages: [1]   Go Up
Print
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.16 | SMF © 2011, Simple Machines Valid XHTML 1.0! Valid CSS!