Forum Moderators: coopster

Message Too Old, No Replies

Create a cool "Page Not Found" script

Can I create a custom Page Not Found script using PHP?

         

cosmoyoda

6:39 am on Nov 17, 2007 (gmt 0)

10+ Year Member



Hi guys...

Hope I can make my point here with just a few words. If you go to Google's site or Apple's site and put /adasdaksjdaskhjdaskjdhasjhkdasjhkdashjkdaskhjda (something random), you will probably be redirected to a custom "Page Not Found". That is a pretty cool web development trick and I would like to ask you guys if you know how to do this using PHP...

When a user visits mysite.com and puts /something that cannot be found in my server he just sees the boring "Server not found" message of their browser. If I could write a custom "Page Not Found" script for my site that would be pretty awesome, but I have no idea how to do this

Thanks a lot in advance!

phparion

7:28 am on Nov 17, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



it has to do with your webserver i.e apache and not php.

use this in your htaccess file

ErrorDocument 404 http://www.example.com/custom_page.html

and make this custom_page.html as beautiful as you want :)

[edited by: eelixduppy at 7:32 am (utc) on Nov. 17, 2007]
[edit reason] changed to example.com [/edit]

cosmoyoda

8:18 am on Nov 17, 2007 (gmt 0)

10+ Year Member



Oh that's so simple!

I thought before that it might have something to do with my Apache server and not PHP.

Thanks a lot, phparion, thats stupidly easy to do. Thanks a bunch!

phranque

9:15 am on Nov 17, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



not so fast!

make sure you:
- specify a local url path to your error document. a canonical url path will cause apache to send an external redirect.
- return a 404 response status code with your error document
- don't do a meta-refresh within your error document

cosmoyoda

9:33 am on Nov 17, 2007 (gmt 0)

10+ Year Member



All I did was create error.html page and include that in my .htaccess file.

I just tested by doing www.MySite.com ( my site ) + /asdjkasldashdasldashldk/ (some random destination) and it returned to that error.html page, because the weird destination doesn't exist.

Did I miss something? What do you mean by "meta-refresh" and "404 response status code"?

The Contractor

11:07 am on Nov 17, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Make sure you use a local and not a full path. Also, use Server Headers [webmasterworld.com] inside your control panel here at WebmasterWorld to check a non-existent page. Make sure it really returns a 404 not found error.

phranque

2:29 pm on Nov 17, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Did I miss something? What do you mean by "meta-refresh" and "404 response status code"?

if you don't have "meta-refresh" somewhere in your page don't worry about it.
however...
be sure that FIRST THING, before all that purdy stuff in your cool not found page, before the doctype or html tag or whatever, you start with the following:
Status: 404\n\n

(those two "\n"s would be 2 newlines)
that way your browser knows that even though it has some content to display, it actually represents content that was not found.
it should leave your url in the address bar pointing to the missing content instead of the url of the error page, for example.
it also tells the search engines not to index that content for that url.

PHP_Chimp

6:35 pm on Nov 17, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



header ("HTTP/1.0 404 Page Not Found");
Will send the 404 response.
Seeing as this is a header you need to call this before any html output.

cosmoyoda

9:51 pm on Nov 17, 2007 (gmt 0)

10+ Year Member



great guys, thanks a lot for the help!
It works like a charm now...