Create useful, informative custom 404 pages for your users with this sNews CMS mod
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.
Comments
RSS Comments Feed
slemborg
Matt
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
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
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
NicuAlecu
John Flower
Matt
John Flower
ErrorDocument 404 http://www.yoursite.com/error.php
or
ErrorDocument 404 http://www.yoursite.com/404
I think that would be the safest way to work with 404's. One thing you don't want is Google filled with pages from your site that don't exist.
Matt
As far as I know, sNews does return the proper header, so there isn't really a need for any specific robots tags.
John Flower