1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Sending 404 response

Discussion in 'Apache' started by Liminal, Oct 15, 2005.

  1. #1
    Hey all,

    I have some places within the site where I need to display custom 404 pages specific to that particular portion of the site. How would I define (perhaps using .htaccess) that a particular page on the site should send a 404 status code back to the client?

    Best,
    James
     
    Liminal, Oct 15, 2005 IP
  2. frankm

    frankm Active Member

    Messages:
    915
    Likes Received:
    63
    Best Answers:
    0
    Trophy Points:
    83
    #2
    If the page does not exist, like /asdfasdfasdf.html: use ErrorDocument 404 /404.html in your .htaccess/httpd.conf. If the page does exists (script) use: (php code)
    
    header("HTTP 404 Not found");
    echo "Page not found ... sorry";
    die();
    
    PHP:
     
    frankm, Oct 15, 2005 IP
    Liminal likes this.
  3. Liminal

    Liminal Peon

    Messages:
    1,279
    Likes Received:
    63
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks frankm but that's not exactly what I was asking for. I'd like to have multiple 404 pages on the site. Say for an e-commerce site with no separate subdomains, each of the product categories could have its own 404 page with wording applicable to that category.

    EDIT: actually, you are right. I can just list the ErrorDocument directive multiple times:

    ErrorDocument 404 /category-one.html
    ErrorDocument 404 /category-two.html

    Thanks!
     
    Liminal, Oct 15, 2005 IP
  4. minstrel

    minstrel Illustrious Member

    Messages:
    15,082
    Likes Received:
    1,243
    Best Answers:
    0
    Trophy Points:
    480
    #4
    Liminal, if all the 404 files are in the same directory and in the same .htaccess file, how will that work? Won't all 404 requests go to the first (or maybe the last) 404 file listed?

    One way to do it would be to put the different sections of the website in different subdirectories, each with its own .htacess file listing the 404 for that directory. To be truthful, I've never had a reason to try this but I think it will work.
     
    minstrel, Oct 15, 2005 IP
  5. Liminal

    Liminal Peon

    Messages:
    1,279
    Likes Received:
    63
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Minstrel, you bring up a great point. I was just thinking that multiple 404 directives in the same .htaccess might be a problem. So, your suggestion re: separate directories with its own .htaccess may be my only option.

    thanks :)
     
    Liminal, Oct 15, 2005 IP
  6. minstrel

    minstrel Illustrious Member

    Messages:
    15,082
    Likes Received:
    1,243
    Best Answers:
    0
    Trophy Points:
    480
    #6
    Post back with the result, okay? I'm curious now to see if it works...
     
    minstrel, Oct 15, 2005 IP
  7. Liminal

    Liminal Peon

    Messages:
    1,279
    Likes Received:
    63
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Dang :) You must spread some Reputation around before giving it to minstrel again.

    Ok, here are the results:

    a) Multiple ErrorDocument 404 directives in the same .htaccess result in the last 404 doc being served
    b) Own ErrorDocument 404 in .htaccess within a specific directory is processed as expected (i.e. it works).
     
    Liminal, Oct 15, 2005 IP
    minstrel likes this.
  8. minstrel

    minstrel Illustrious Member

    Messages:
    15,082
    Likes Received:
    1,243
    Best Answers:
    0
    Trophy Points:
    480
    #8
    Cool. :D

    Now I know the answer too if I ever need it.

    In fact, now that I think about it, I could make use of that... to generate different error pages for the forum, directory, and main site at PsychLinks.

    Thanks, Liminal! :)
     
    minstrel, Oct 15, 2005 IP