By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,948 Members | 664 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,948 IT Pros & Developers. It's quick & easy.

Setting up a page that will intercept all pages that do not already exist?

P: n/a
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
Jul 17 '05 #1
Share this Question
Share on Google+
19 Replies


P: n/a
In comp.lang.php Joshua Beall <jb****@donotspam.remove.me.heraldic.us> wrote:
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.


It's called a 404 handler, but it doens't have to return an http error
status. In the 404 handler script you can either return a 404 (the
default status) or choose to do anything else you might come up with,
like redirecting to the correct url in case of small typos. The client
will never find out it was handled by the 404 handler...

Jul 17 '05 #2

P: n/a
On Tue, 07 Dec 2004 21:29:00 GMT, "Joshua Beall"
<jb****@donotspam.remove.me.heraldic.us> 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!

This is really more of a webserver question and it really depends on
the webserver you're using. You mention .htaccess, so I'll assume
apache.

edit the httpd.conf file (/etc/httpd/conf/httpd.conf

and look for this:

#ErrorDocument 404 /missing.html
Uncomment it and change the url to what you want.

restart the server
--
gburnore@databasix dot com
---------------------------------------------------------------------------
How you look depends on where you go.
---------------------------------------------------------------------------
Gary L. Burnore | ۳ݳ޳ݳۺݳ޳ݳݳ޳ݳ۳
| ۳ݳ޳ݳۺݳ޳ݳݳ޳ݳ۳
DataBasix | ۳ݳ޳ݳۺݳ޳ݳݳ޳ݳ۳
| ۳ 3 4 1 4 2 ݳ޳ 6 9 0 6 9 ۳
Black Helicopter Repair Svcs Division | Official Proof of Purchase
================================================== =========================
Want one? GET one! http://signup.databasix.com
================================================== =========================
Jul 17 '05 #3

P: n/a
"Daniel Tryba" <sp**@tryba.invalid> wrote in message
news:41***********************@dreader3.news.xs4al l.nl...
In comp.lang.php Joshua Beall <jb****@donotspam.remove.me.heraldic.us>
wrote:
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.


It's called a 404 handler, but it doens't have to return an http error
status. In the 404 handler script you can either return a 404 (the
default status) or choose to do anything else you might come up with,
like redirecting to the correct url in case of small typos. The client
will never find out it was handled by the 404 handler...


Ah ok.

Are there other ways to do it? What are they? Do they provide advantages
of some sort? Is there any reason to not go the 404 handler route?
Jul 17 '05 #4

P: n/a
>>This is in a LAMP environment, and does not need to be portable to Windows
or any other webserver.


This is really more of a webserver question and it really depends on
the webserver you're using. You mention .htaccess, so I'll assume
apache.


The 'A' in LAMP stands for Apache.

Are their other ways to do this sort of thing, and if so, do they provides
advantages over the 404 handler method?
Jul 17 '05 #5

P: n/a
You could place redirect index.php scripts using the header() function
in each of those mystery folders if they are specific known ones,
otherwise the .htaccess solution doesn't appear to be an error to the
user, it just proceeds to the page you specify instead of showing the
error message. The faulty url actually remains in the address bar, at
least with mozilla.

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

Jul 17 '05 #6

P: n/a
Joshua Beall <jb****@donotspam.remove.me.heraldic.us> wrote:
[404 handler]
Ah ok.

Are there other ways to do it? What are they? Do they provide advantages
of some sort?
You could use the rewriteengine to redirect all requests to 1 script.
http://httpd.apache.org/docs/misc/rewriteguide.html
Is there any reason to not go the 404 handler route?


Can't think of any.

Jul 17 '05 #7

P: n/a
Rob

"Joshua Beall" <jb****@donotspam.remove.me.heraldic.us> schreef in bericht
news:wgptd.1810$N%6.99@trnddc05...
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


When using a .htaccess file you only catch the 404 error and you don't need
to "give the 404 error"(?). As a response you can redirect to any page
without letting the user know it was a 404 error. i.e.:

[.htaccess]
# "404 Not Found",
ErrorDocument 404 http://mysite.com/pages/display.php
HTH Rob
Jul 17 '05 #8

P: n/a
d
hi!

mod_rewrite in Apache supports what you want to do. I'm not going to tell
you exactly how to do it - you should read the docs yourself if you want to
go this route.

http://httpd.apache.org/docs/mod/mod_rewrite.html

d.

ok, two hints - "RewriteCond" and "-f" :)

"Joshua Beall" <jb****@donotspam.remove.me.heraldic.us> wrote in message
news:wgptd.1810$N%6.99@trnddc05...
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

Jul 17 '05 #9

P: n/a
On Wed, 08 Dec 2004 08:55:22 GMT, "Rob" <reply_@news_group.please> wrote:

"Joshua Beall" <jb****@donotspam.remove.me.heraldic.us> schreef in bericht
news:wgptd.1810$N%6.99@trnddc05...
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


When using a .htaccess file you only catch the 404 error and you don't need
to "give the 404 error"(?). As a response you can redirect to any page
without letting the user know it was a 404 error. i.e.:

[.htaccess]
# "404 Not Found",
ErrorDocument 404 http://mysite.com/pages/display.php
HTH Rob


To take this a step beyond 404 : If there are specific pages which you know to
no longer exist (as opposed to random 404 errors), it's probably wiser to return
a 301 and direct the user agent to the new page automagically. This will, in
theory, cause proxies and search engines to update their old links to the new
page, as opposed to simply deleting them.

For your example :

[.htaccess]
redirect 301 /pages/contact http://mysite.com/pages/display.php

As others have mentioned, an Apache solution is more appropriate in this case
than something scripted in PHP.

HTH !

Jul 17 '05 #10

P: n/a
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.

Jul 17 '05 #11

P: n/a
.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
Jul 17 '05 #12

P: n/a
>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
Jul 17 '05 #13

P: n/a
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.
Jul 17 '05 #14

P: n/a
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.

Jul 17 '05 #15

P: n/a
Michael Fesser <ne*****@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).

Jul 17 '05 #16

P: n/a
.oO(Daniel Tryba)
Michael Fesser <ne*****@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
Jul 17 '05 #17

P: n/a
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
Jul 17 '05 #18

P: n/a
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.

Jul 17 '05 #19

P: n/a
.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
Jul 17 '05 #20

This discussion thread is closed

Replies have been disabled for this discussion.