BubblesSOC

Hi I'm Bubs, welcome to my home on the web!

Thanks for stopping by! You have stumbled upon the personal website of Sidney Collins (aka Bubs). I'm a 27-year old student at the University of Alabama studying Computer Science and Mathematics. When I'm not busy programming you'll probably find me writing, drawing, reading, shopping, eating, or playing Monopoly.

Read More


Feb 9

Custom PHP 404 Pages

I wanted to integrate my 404 page into my skins, but I needed it to be a .php file instead of .shtml (which is the default filetype I believe). So here’s how I did it.Open your existing .htaccess file. If you do not have one already, create a new file and save it as .htaccess. Add the following code to the file:

ErrorDocument 404 http://www.bubblessoc.net/404.php

You can change 404.php to anything you want. It can be error.php or mysiterox.php. And of course, you use your URL instead of mine ;)

I’ve also integrated custom 404 pages for my subdomains as well using the same method. This is useful if you have hostees who want their own error pages. Just make an .htaccess file for each of your subdomains. Otherwise, the root .htaccess will take precedence.

If you want to display the name of the page that issued a 404 use:

<?php echo $_SERVER['REQUEST_URI'] ?>

Pretty nifty I think.


Post Info

Bubs posted this entry 6 years ago on Thursday, February 9th, 2006 at 10:11 pm.


Comments So Far

6 people have commented on the post “Custom PHP 404 Pages.” Why don't you leave a comment?

  1. Meredith's Gravatar

    Meredith (Quote, Reply)

    Oh wait, LOL, I tried it and it’s not what I meant to look for. For some reason when users get a 404 on my site, instead of seeing the URL that wasn’t there still in the Location bar, they see the 404 page’s own URL. I haven’t figured out how to make the 404 not forward to the error page (losing the bad URL) or how to display the unfound page on the error page.

    Fri Feb 10th, 2006 7:51 pm

  2. Meredith's Gravatar

    Meredith (Quote, Reply)

    Oh wow, I was looking for exactly that (how do you print the page that caused the error) earlier today. Thanks!

    Fri Feb 10th, 2006 7:46 pm

  3. Dayna's Gravatar

    Dayna (Quote, Reply)

    I put a 404.php file in my themes folder for my error pages, so I don’t have to edit any :-P

    Mon Feb 13th, 2006 7:43 am

  4. Bubs's Gravatar

    Bubs (Quote, Reply)

    That’s odd :confused: I’ve never run across that problem.

    Mon Feb 13th, 2006 5:50 pm

  5. pbhj's Gravatar

    pbhj (Quote, Reply)

    Meredith, try not using the fully qualified URI – that causes apache to NOT send the error data as it thinks it’s going to an external URI.

    eg

    ErrorDocument 404 /mydocs/errrors/404.php

    Mon Mar 6th, 2006 6:27 pm

  6. The Mikeness's Gravatar

    The Mikeness (Quote, Reply)

    make sure your error handling page headers out the proper status code if necessary as well. I wrote a catchall page that I use for all ErrorDocument’s for all the status codes for which Apache will actually serve your custom page, it uses the $_SERVER['REDIRECT_STATUS'] variable that is set in PHP when the user is redirected to an error page.

    You could possibly also do something like this:

    httpd.conf:
    ErrorDocument 403 /error/error.php?code=403

    error.php:

    (an array, $bob[403]= “403 Forbidden”)
    header(‘Status: ‘.$bob[$_GET['code']]);

    or substitute $_GET['code'] with the almighty $_SERVER['REDIRECT_STATUS'] (works with apache2 as a module, beyond that i cant guarantee anything)

    I also have an array of messages for each error code, and its easy to build a MySQL logger to grab things that the likes of LogWatch cant, for example:

    $_SERVER['HTTP_HOST']
    $_SERVER['REDIRECT_STATUS']
    $_SERVER['REDIRECT_REQUEST_METHOD']
    $_SERVER['SERVER_PORT']
    $_SERVER['REQUEST_URI']
    $_SERVER['REDIRECT_URL']
    $_SERVER['REMOTE_ADDR']

    $_SERVER['HTTP_USER_AGENT']
    $_SERVER['HTTP_COOKIE']
    $_SERVER['HTTP_ACCEPT']
    $_SERVER['HTTP_ACCEPT_ENCODING']
    $_SERVER['REQUEST_METHOD']
    $_SERVER['QUERY_STRING']

    Fri Oct 31st, 2008 10:15 am

Leave a Comment

Leave a Comment

Name, email, and comment required. Email never displayed. Upload a Gravatar to be displayed with your comment. Comments containing links will be moderated for spam prevention.

«
»

Search

Search the Archives

Hop to the Top