This is a discussion on Setting up a page that will intercept all pages that do not already exist? within the PHP Language forums, part of the PHP Programming Forums category; .oO(Andrew) >Using the ErrorDocument 404 syntax to redirect everything not found to >a PHP script will issue ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
.oO(Andrew)
>Using the ErrorDocument 404 syntax to redirect everything not found to >a PHP script will issue a 404 header to the browser, Not necessarily. In case of an error the server will call the error document. If that is a static HTML document it will be sent with an error code back to the user agent. If it's a script you can do whatever you want. If you want to redirect to another address the user agent will get the redirection code, but not the error code (there can be only one status code in the response header). >but none of the >browsers I have tried, Opera, Firefox or IE show an error page provided >the header is followed by html. They don't show an error because they don't receive an error code. Micha |
|
|||
|
>Using the ErrorDocument 404 syntax to redirect everything not found to
>a PHP script will issue a 404 header to the browser, but none of the >browsers I have tried, Opera, Firefox or IE show an error page provided >the header is followed by html. > >If it works don't knock it, and you can always combine the method with >some static html caching. > >The only other alternative I can think of is to use mod_rewrite in >apache somehow. According to the Apache manual: ErrorDocument 404 /missing.php sends the result of running missing.php back to the browser as though it were from the nonexistent page. ErrorDocument 404 http://www.site.domain/missing.php sends a REDIRECT to the browser, even if the two statements above refer to the same script on the same host. The two are NOT equivalent. The first leaves the bad link in the browser address bar; the second updates it. The first gives the REDIRECT_* variables if the error document is a CGI or PHP script; the second does not. The first refers to a local script; the second might refer to a different server. It is worth nothing that if you use ErrorDocument 404 (with Apache) to redirect to a local PHP script, using the first form of the ErrorDocument above (no http:) you get some useful variables handed to you which can be useful for logging for perusal by the webmaster or handling the error (e.g. try to match the request to the URL closest in spelling to it.). $_SERVER['REDIRECT_URL'] is the path/filename of the bad request on the local server relative to the document root. $_SERVER['REDIRECT_STATUS'] is the error code, e.g. 404 $_SERVER['REDIRECT_ERROR_NOTE'] gives some indication of the error (e.g. what file was missing) $_SERVER['HTTP_REFERER'] (no REDIRECT_ in front of this one) is the URL of the page with the bad link on it. $_SERVER['REDIRECT_REQUEST_METHOD'] GET, POST, or whatever to the bad link. A CGI or PHP script should be able to give a Status: header to override any error code in the response. It's my opinion that the status code coming back from an error document is the status code of the error document (e.g. not 404 unless the document goes out of its way to return that with a Status: header) for the local script case, and a redirect code when ErrorDocument is given a full URL with http:. My browser is not cooperative enough to let me verify this information, though. Gordon L. Burditt |
|
|||
|
Joshua Beall wrote:
> Hi All, > > I would like to setup my site so that when a page is request that does not > exist, transfer is instead passed to a certain page that I specify. For > instance, if the user requests http://mysite.com/pages/contact and that is > not a valid page or directory, then the request is handed to > http://mysite.com/pages/display.php or something like that. > > I am wondering what the best way to do this is - I know that I could use an > .htaccess file to redirect 404 errors, but I would rather not give a 404 > error in the first place. Is there a better/other way? I seem to remember > having read about another way... but I can't seem to remember... > > This is in a LAMP environment, and does not need to be portable to Windows > or any other webserver. > > Any help would be greatly appreciated! Have you seen the thread I started, "Include" security?, I think you're looking for the same thing I am. The script in the reply I replied to lets you set a 404 page, or you could set it to any page. |
|
|||
|
I have experienced a few web hosts that will not allow you to redirect
404's to a PHP script, you receive a message saying something like "A 404 error occured additionally an error document was not found the handle the request" Also remember, any redirection you do will lose the POST data, but you can retreive the GET data using a piece of code like this: if (isset($_SERVER['REDIRECT_QUERY_STRING'])) { $redirected_queries = explode('&', $_SERVER['REDIRECT_QUERY_STRING']); foreach ($redirected_queries as $redirected_query) { $redirected_query = explode('=', $redirected_query, 2); $_GET[urldecode($redirected_query[0])] = urldecode($redirected_query[1]); } unset($redirected_queries, $redirected_query); } This will import all the redirected GET variables into the $_GET global (there shouldn't be any in there to overwrite though). Oh and the double urldecode is because & and = may be present in the data itself, so you can't really do the urldecoding before exploding. |
|
|||
|
Michael Fesser <netizen@gmx.net> wrote:
>>but none of the >>browsers I have tried, Opera, Firefox or IE show an error page provided >>the header is followed by html. > > They don't show an error because they don't receive an error code. But they _will_ receive the error code. IMHO only IE is so silly to display it's own error page under certain conditions (less than x bytes IIRC). |
|
|||
|
.oO(Daniel Tryba)
>Michael Fesser <netizen@gmx.net> wrote: >>>but none of the >>>browsers I have tried, Opera, Firefox or IE show an error page provided >>>the header is followed by html. >> >> They don't show an error because they don't receive an error code. > >But they _will_ receive the error code. OK, my statement above was misleading (I should read the posts more carefully). Of course browsers will receive the error code, if the error document is delivered as-is or doesn't send its own status header. But if the error document is a script and redirects to another address the browser will get only the redirection code. Micha |
|
|||
|
Joshua Beall wrote:
> Hi All, > > I would like to setup my site so that when a page is request that does not > exist, transfer is instead passed to a certain page that I specify. For > instance, if the user requests http://mysite.com/pages/contact and that is > not a valid page or directory, then the request is handed to > http://mysite.com/pages/display.php or something like that. > > I am wondering what the best way to do this is - I know that I could use an > .htaccess file to redirect 404 errors, but I would rather not give a 404 > error in the first place. Is there a better/other way? I seem to remember > having read about another way... but I can't seem to remember... > > This is in a LAMP environment, and does not need to be portable to Windows > or any other webserver. > > Any help would be greatly appreciated! > > His, > -Josh > > You would just setup the 404 redirect for any arbitrary page, it doesn't have to be an error page. It's good to know in your logs that people are receiving 404s even if they never realize it on their end, so you can be aware that you may have bad links somewhere. Hope this helps. Adam |
|
|||
|
There is a good reason to not use the 404 handler.
Try using W3C's validator service (http://validator.w3.org/) on a page using this method. It will throw you an error saying the page could not be validated because it was not found. My point is, you can't judge how services like this or search engines will react. |
|
|||
|
.oO(Andrew)
>There is a good reason to not use the 404 handler. >Try using W3C's validator service (http://validator.w3.org/) on a page >using this method. www.php.net uses error handlers to allow URL shortcuts and it works. >It will throw you an error saying the page could not be validated >because it was not found. I would guess that there's something wrong with the server then. If done properly the requesting user agent won't notice that the delivered content is the output of an error handler. >My point is, you can't judge how services like this or search engines >will react. Sure, but there's not much a difference to any other user agents. Request -> response, request -> response. It's always the same, even if some UAs or servers might send some strange headers. Micha |