#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    6
    Rep Power
    0

    Issues / confusion over issuing content AND 301/404


    I've been trying to figure out the proper way to handle pages that no longer exist on a website. Rather than have a ugly default "404 not found", I'm looking to have a decent page result (one with links back to the rest of the site AND ALSO issue a proper 404 or 301 header response so that Search Engines will realize that the result is no longer available and can be dropped from indexes).

    Sounds simple enough ...but php.net states that you cannot use a Header() response AFTER delivering content to the browser. My Host admin allows me to specify a custom 404 Not found file ...but doing so only seems to change the result from a 404 to a 301 - Moved Permanently. Obviously, this will not work as Search Engines will then see the custom error page as duplicate content for every page that really no longer exists.

    I've looked at .htaccess - which can be used to specify a ErrorDocument 404
    That works fine for apache but my live host is running windows servers / with isapi rewrite.


    So would one achieve something like this?
    http:// php. net/manual/en/thisdoesntexist

    ( Try it, it'll issue a 404 header AND also displays page content to user)
  2. #2
  3. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,965
    Rep Power
    9397
    Originally Posted by stride-r
    I've been trying to figure out the proper way to handle pages that no longer exist on a website. Rather than have a ugly default "404 not found", I'm looking to have a decent page result (one with links back to the rest of the site AND ALSO issue a proper 404 or 401 header response so that Search Engines will realize that the result is no longer available and can be dropped from indexes).
    You shouldn't be sending a 401 unless you actually need HTTP authentication for a user. Which I'm very sure you don't.

    Originally Posted by stride-r
    Sounds simple enough ...but php.net states that you cannot use a Header() response AFTER delivering content to the browser. My Host admin allows me to specify a custom 404 Not found file ...but doing so only seems to change the result from a 404 to a 301 - Moved Permanently. Obviously, this will not work as Search Engines will then see the custom error page as duplicate content for every page that really no longer exists.
    The clever ones might recognize that a whole bunch of URLs are redirecting to a particular page and guess that it's a 404 page, but otherwise they'll just see it as a redirect. 301 means that the destination page is the correct page for some (non-existent) URL - no duplicate content.

    Originally Posted by stride-r
    So would one achieve something like this?
    Depends on your site, the framework, the setup, a few things. If you have some sort of CMS or site framework then that's where you would configure a 404 action. Otherwise the Apache solution is generally an ErrorDocument and for IIS... There's some web.config settings you can use but I'm not 100% sure what they are and whether they entail an actual redirect.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    6
    Rep Power
    0
    First off, thanks for the reply!!!

    Originally Posted by requinix
    You shouldn't be sending a 401 unless you actually need HTTP authentication for a user. Which I'm very sure you don't.
    Sorry, you're right. That was a typo - I meant "301" in the above post. I now corrected it.

    Originally Posted by requinix
    301 means that the destination page is the correct page for some (non-existent) URL - no duplicate content.
    Are you sure that's right?? 301, as far as I've ever heard is intended to mean "the destination page is now the correct page". That makes sense for a MOVED page/s ...but not for 50-100 products which all no longer exist.

    Originally Posted by requinix
    If you have some sort of CMS or site framework then that's where you would configure a 404 action.
    Yes, that's what i was hoping to do - via php and header("Status: 404 Not Found"). However, php . net site states that header cannot be delivered after sending content to browser ...and a redirect after issuing header Status 404 does not work.

    ...so the question still stands - How does one "configure a 404 action" in php??

    Originally Posted by requinix
    Otherwise the Apache solution is generally an ErrorDocument
    I use htaccess on some sites but a few are hosted on windows servers. I'll search further for iis..
  6. #4
  7. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,965
    Rep Power
    9397
    Originally Posted by stride-r
    Sorry, you're right. That was a typo - I meant "301" in the above post. I now corrected it.
    That makes a lot more sense

    Originally Posted by stride-r
    Are you sure that's right?? 301, as far as I've ever heard is intended to mean "the destination page is now the correct page". That makes sense for a MOVED page/s ...but not for 50-100 products which all no longer exist.
    Yeah, basically. I wasn't saying you should use a 301 for missing pages, just trying to clarify the whole "they'd be duplicate content" thing. You should totally use a 404.

    Originally Posted by stride-r
    Yes, that's what i was hoping to do - via php and header("Status: 404 Not Found"). However, php . net site states that header cannot be delivered after sending content to browser ...and a redirect after issuing header Status 404 does not work.

    ...so the question still stands - How does one "configure a 404 action" in php??
    You decide that the content doesn't exist before trying to show anything. Really, if there's no content then what are you outputting?

    When a "page" doesn't exist then (for example) abort what you were doing, use the header(), and show something appropriate for a 404 page.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    6
    Rep Power
    0
    Originally Posted by requinix
    You decide that the content doesn't exist before trying to show anything. Really, if there's no content then what are you outputting?
    The point was to show "something" ...like a basic "requested document no longer exists" message ...but include basic site appearance styling and maybe a main menu, etc. so user can continue.

    Originally Posted by requinix
    When a "page" doesn't exist then (for example) abort what you were doing, use the header(), and show something appropriate for a 404 page.
    Ah, yes ...but that's the quandary. Using php header("HTTP/1.0 404 Not Found") does not have a provision for redirection afterward. Only Header("Location: ...") does that, right? However, unless i'm reading it wrong, header("Location:") also only delivers 302 status ....which doesn't solve the issue of providing the real user with something to work with and yet give search engines the 404 so they know the requested url is not valid any more.
  10. #6
  11. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    Hi,

    I think there's some confusion ...

    First of all, if you want to emphasize that a particular resource is no longer available, you'd use a "410 Gone". A 404 just tells the user that there's no resource, regardless of whether there was yesterday or there has never been one.

    You can indeed setup 410 pages for PHP scripts by simply replacing the original content with an error page content and a 410 header. But this is pretty cumbersome and inefficient compared to letting the webserver take care of this (and actually removing the old scripts).

    If you're looking to setup a generic 404 page, this is only possible on your webserver. No PHP script can do this. If your Microsoft webserver doesn't support custom error pages, you cannot have them with your current setup.

    Using PHP only works for a very specific setup and for a subset of the requests: If you have a (MVC) framework with a central "app.php" script, which the webserver uses as a "proxy" for certain requests, then those requests (and only those) can be handled by PHP.

    To wrap it up:
    • Setting up error pages is generally up to the webserver
    • If the webserver routes some requests to an "app.php" script, PHP is responsible for displaying 404 pages for those requests
  12. #7
  13. Wiser? Not exactly.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    May 2001
    Location
    Bonita Springs, FL
    Posts
    5,932
    Rep Power
    4033
    Originally Posted by stride-r
    Ah, yes ...but that's the quandary. Using php header("HTTP/1.0 404 Not Found") does not have a provision for redirection afterward. Only Header("Location: ...") does that, right?
    header() in general does not allow you to do anything after content is sent, that goes equally for sending a HTTP response or a Location: header.

    Your application should be structured in such a way that you can make the determination of whether a given resource exists and then respond with the appropriate header before you attempt to send any sort of content response.

    Something like:
    Code:
    <?php
    
    $content = getContentFromDB($_GET['page']);
    if (!$content){
       header('HTTP/1.0 404 Not Found');
       include('404content.php');
    }
    else {
       echo $content;
    }
    In order for something like that to be most effective however you need to be able to have your server pass the request to PHP without it sending a redirect to the end-user. How this is done varies from server to sever.
    Recycle your old CD's, don't just trash them



    If I helped you out, show some love with some reputation, or tip with Bitcoins to 1N645HfYf63UbcvxajLKiSKpYHAq2Zxud

IMN logo majestic logo threadwatch logo seochat tools logo