Create useful, informative custom 404 pages for your users with this sNews CMS mod
filed under: sNews CMS / Hacks & Mods
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.
6 comments
Add a new comment »Categories
Recent Entries
Recent Comments
- Dede (I checked it today in a shop. GT2 had some troubles with six ...)
- Matt (Bintang, You need to re-direct the url, try ...)
- jesth (Ohh.. why didn't I think of that, thanks alot.)
- Matt (Dede, I don't have Gran Turismo 2, any of the 2nd+ generation ...)
- Matt (Jesth, Just change the if condition, instead of looking for ...)
- jesth (Hi (again) Was wondering, is it possible to make it ...)
- Bintang Sembilan (Matt, thanks for your modd. I have apply it to my ...)
- Dede (Hello there. Can you check something for me? I want to buy ...)
- Matt (I think it's a driver issue Terrence, or it was a driver issue. ...)
Popular Entries
- Light-weight related articles mod for sNews 1.7 (4.5/5)
- Image / math hybrid captcha version 2, vastly improved (4.42/5)
- 1024x600 netbook wallpapers of Evangeline Lilly (4.4/5)
- Compact archives for sNews 1.7 (4.4/5)
- sNews Ajax Polls mod now available (4.38/5)
- Pretty date and comments bars in sNews CMS (4.35/5)
- Page caching mod for sNews 1.7 (4.33/5)
- Gravatar mod for sNews 1.7 (4.29/5)
- An improved tag cloud for sNews 1.7 (4.29/5)
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
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!
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.
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
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.
Jun 3rd, 2010 at 3:08 am
Nice one, Matt, thanks.