In this seemingly infinite Web Oh! young browser thou slips In the middle of a thousand trips Realising what was just The apparent reality of pages Is nothing but a veil of certainity 404 alone is the absolute reality -Arun
So a visitor to your website mistypes a URL, what does he see? A nearly blank page with ‘404 error’ emblazoned over it? At least that was my case until I decided to do something about it.
So many great tutorials have been written on creating a 404 Error page. The best in my opinion is at SacramentoWeb. However, the solution the author has proposed leads the user to a search box already filled with a value by second guessing what the user tried to search for. I was looking for a 404 page that already has those results below the search box.
After some research, I finally hacked up some PHP code. The result can be already seen on my site and looks quite impressive. I mean I was quickly addicted to misusing the feature by performing random searches e.g. for hollywood stuff on my site I would try typing !!!
Here is a quick summary of what to do for any template to display these results:
.htaccess file, then open it and copy paste the following as your first lineErrorDocument 404 /index.php?error=404
Copy index.php from your template and rename it to 404.php in the same directory
Remove all sections dealing with posts or comments. Roughly the lines between < ?php if ( have_posts() ) and “
Copy the following line to the first line of 404.php. This is to help search engine spiders indicate that this is an error page.
header(”HTTP/1.0 404 Not Found”);
You can write pretty much anything in this page. Or you can download my 404 page from here
Thats it!
Check out the results, I’m sure you (and your visitors) would be able appreciate the improvements in the browsing experience.
Bookmark this to del.icio.us | Digg | Furl | Netscape | Google Bookmarks | Technorati | reddit |
14 Responses
Tags at Is there food?
March 28th, 2006 at 5:35 pm
1[...] age for the category you were trying to visit (thanks, in part, to the information I found here). Apologies for this, but I [...]
Justin Perkins
April 26th, 2006 at 10:27 am
2Just noticed your article, wanted to point out that when you say this:
“I was looking for a 404 page that already has those results below the search box.”
The article you linked to answers with this:
“Why not automatically search, you ask? As much as I like that feature of Mike Davidson’s site, I am concerned with the cost this comes at. Do we really want to search the entire database (searching is expensive) on just about anything the visitor types in? I don’t.”
chris
April 26th, 2006 at 4:22 pm
3Thank you for this information, which I have used to put together my own wordpress 404 page. I just wanted to let you know, that instead of hardcoding the site URL (like you do in line 19 in the above code fragment), you can get the URL from wordpress like this:
$searchurl = getbloginfo(’url’) . ‘/?s=’;This template tag is documented in the codex: http://codex.wordpress.org/TemplateTags/getbloginfo
Regards
Arun bhai
April 26th, 2006 at 5:06 pm
4@justin:
I understand the author’s concern on page loads. But I belive the behavior is as desired. So the user would not be dissappointed
@chris:
Thanks a lot! I have modified my code as per your suggestion. It looks more general purpose now
overla
June 14th, 2006 at 8:02 pm
5Hi Arin, thanks for this code, it works really well. Only thing is i had to delete line 20: $searchurl = getbloginfo(’url’) . ‘/?s=’; because i kept getting an error: Fatal error: Call to undefined function: getbloginfo() in /my/url/theme/404.php on line 20
So my 404 page works without that line, but not if there is a long incorrect URL typed in, then it doesnt search for anything.
Wonder if you could offer any help? Thanks again
overla
June 14th, 2006 at 8:35 pm
6sorry, one last thing, which i think might be the problem, how can i get the script to drop the / at the end of a url - because it doesnt search if there is a / attached to the end. The reason i am trying to fix this is because i changed my permalink structure, so all of my pages listed on yahoo search engine for example , point to my 404 page with a url ending in /, so im trying to get the script to find the pages. thanks again
RAB
January 7th, 2007 at 3:22 am
7Good article and should be pretty handy for all. Herez another simple trick to do it http://webtips.blogsome.com/2006/11/19/customize-404-page-free-blogs/
But I am bit confused with this http://wordpress.org/support/topic/96927 Any thoughts?
Madison
March 12th, 2007 at 9:07 pm
8Nice Site!!! (p)
Mark
March 31st, 2007 at 1:21 pm
9Your demonstration link (…/blog/python) does not work (anymore?). It shows a simple, standard Wordpress (or maybe K2) 404 page. But I like your idea and will try on my blog.
Jaki Levy
June 27th, 2007 at 1:45 am
10Nice tip - I customized the 404 page to include archives and popular posts. What I wanted to do even further was to redirect people with a meta refresh. I recently changed my permalink structure and now people are looking for things that are not there.
So when users go to OldARTICLElink, I want to redirect them to NewARTICLElink.
My old permalink structure was category/post-title
Now it’s date/category/post-title
Any idea on how to do this?
metablog » 404 errors on blogs
June 27th, 2007 at 10:19 am
11[...] BTW - this is a good idea for any site you have - you should ALL edit/customize your 404.php page [thanks to Arun’s Blog for the great tips] [...]
Apache Dude
August 26th, 2007 at 2:05 am
12What about other HTTP status codes? Like ErrorDocument 500 /index.php?error=500, etc… ? All 57 Status Codes
archGFX | Adding Search results to Wordpress 404 pages
September 13th, 2007 at 4:15 am
13[...] UPDATE: Some additional code was used from Arun’s nearly perfect 404. [...]
BH
November 3rd, 2007 at 10:34 am
14Awesome tip. I put it in and it works awesomely.
I’m using Triple K2 and it works seamlessly.
RSS feed for comments on this post · TrackBack URI
Leave a reply
Categories
Recent Posts
Recent Comments
Meta
Bookmarks
My Twitter
Site Search
ArunRocks is proudly powered by WordPress - BloggingPro theme by: Design Disease