Create useful, informative custom 404 pages for your users with this sNews CMS mod

Comments (6)

Custom 404 pages is old news, you can make your own in CPanel, but they're rarely useful for anything other than integrating into your site's design. A while back Google released a drop-in javascript 404 Widget, I tried it out, it was neat, but it hasn't learned much at all about my site in the 6 months I tried it, therefore reducing it's overall usefulness to me.

So what am I to do? Make my own kickass 404 page generator, custom-tailored to sNews 1.7 of course. If you check out the Google 404 widget link above, and then hit one of my 404 error pages, you'll notice it looks nearly identical.

So what exactly does this little mod really do?

  • First it will scan the database and find and return the closest match (on publicly viewable articles/pages only, of course). This works awesome if you've changed an article's category or title and forgotten to put a 301 redirect in place. This also helps immensely if someone links to you and malforms or incorrectly types a url.
  • Secondly, it will provide an additional menu of options to assist the user;
    • If the best match is within a category, it will give that category link as an alternative.
    • It will return a link to the sitemap.
    • It will return a search form prefilled with suggestions based on the words typed in the bad url.

STEP 1) Back-up your snews.php file, now find function center(), inside that, find the switch case that handles the 404 page output and insert the highlighted code (between the 404 language call and the break) like below;

case '404':
    echo l('error_404'); 
    $query_articles = 'SELECT a.seftitle,c.seftitle AS csef,x.seftitle AS xsef
                FROM  '._PRE.'articles'.' AS a
                LEFT OUTER JOIN '._PRE.'categories'.' as c
                    ON category = c.id
                LEFT OUTER JOIN '._PRE.'categories'.' as x
                    ON c.subcat = x.id AND x.published =\'YES\'
                WHERE a.position > \'0 \' AND a.visible = \'YES\' AND a.published = \'1\' AND a.extraid = \'\'';
    $result = mysql_query($query_articles);
    $similar = array();
    $catsuggest = array();
    while ($r = mysql_fetch_array($result)) {
    unset($uri,$key);
        if ($r['csef']) $uri = $r['xsef'] ? $r['xsef'].'/'.$r['csef'].'/' :  $r['csef'].'/';
        $key = similar_text($_SERVER['REQUEST_URI'],$r['seftitle']);
        $link = '<a href="'._SITE.$uri.$r['seftitle'].'/">'._SITE.$uri.$r['seftitle'].'/'.'</a>';
        $similar[$key] = $link;
        $catsuggest[$key] = $uri;
     }
    krsort($similar);
    $match = array_shift($similar);
    krsort($catsuggest);
    $catsuggest = array_shift($catsuggest);
    $suggestkeywords = preg_replace('/[^a-zA-Z0-9]/', ' ', $_SERVER['REQUEST_URI']);
    echo '<h6>Closest match: '.$match.'</h6><br />';
    echo '<h6>Other things to try:</h6>';
    echo '<ul>';
    if ($catsuggest) {
        echo '<li>Go to: <a href="'._SITE.$catsuggest.'">'._SITE.$catsuggest.'</a></li>';
    }
    echo '<li>Go to the sitemap: <a href="'._SITE.'sitemap/">'._SITE.'sitemap/</a></li>';
    echo '<li>Search <strong>'._SITE.'</strong></li></ul>';
    echo '<form id="search_engine" method="post" action="'._SITE.'" accept-charset="'.s('charset').'">
    <fieldset><input class="text" name="search_query" type="text" id="keywords" value="'.$suggestkeywords.'" />
    <input class="searchbutton" type="submit" value="'.l('search_button').'" /></fieldset>
    </form>';
    break;

That's it, now you'll have smart 404 pages, customized for your sNews install.

bookmark / share this: Bookmark and Share
rated 5/5 (2 votes)


6 comments

Add a new comment »

slemborg slemborg said:
Jun 15th, 2009 at 6:15 pm

Nifty indeed, I will try it out on my testsite, and maybe add it to my mainsite, good job Matt


Matt Matt said:
Jun 17th, 2009 at 12:20 pm

Cool, I just hopped on your site there, so you like Windows 7 so far?

I'm hearing good things about it, I will wait until a public release though, before I put it on my netbook, I'm afraid of losing data, I have everything on that little laptop!


slemborg slemborg said:
Jun 17th, 2009 at 4:49 pm

On my Windows 7 machine, which have been running Windows 7 since the official RC release, is running faster than my tweaked Windows XP machine, tweaked meaning, useless services, etc. etc. removed (nlited)

Both machines are running with the exact same hardware, and i'm truly amazed how Windows 7 is overhauling my XP machine.

For instance, I backed up a dvd, packed it using winrar, in 50MB part sizes.

Unrared on XP ~ 4-6 minuttes
Unrared on Win7 ~ 1 minute and 13 seconds

Both running with 2GB ram
2.4GHz core 2 duo cpu.

So you can imagine, how amazed I am
and all programs are running flawless.


Poppoll Poppoll said:
Jun 27th, 2009 at 9:24 pm

Matt,

You forgot a step 2 Change the lang.php file for the first output line: Oops, we couldn\'t find the page you\'re looking for.

And to have it css valid, move the closing above echo '
Thanks for this mod
Have a look at mine.
PP


Matt Matt said:
Jun 28th, 2009 at 8:48 am

Ahhh, good catch PP, I will update the tutorial now to get the form out of the list, thanks.


NicuAlecu NicuAlecu said:
Jun 3rd, 2010 at 3:08 am

Nice one, Matt, thanks.



Write a comment

* = required field

:

:

:

:

You may insert urls in plain text, urls will be automatically linkified for trusted users and on seasoned posts only. All first comments are moderated, so use your email if you want to be remembered.


Back to top