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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Joshua Beall

    #1

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

    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


  • Daniel Tryba

    #2
    Re: Setting up a page that will intercept all pages that do not already exist?

    In comp.lang.php Joshua Beall <jbeall@donotsp am.remove.me.he raldic.us> wrote:[color=blue]
    > 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.[/color]

    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...

    Comment

    • Gary L. Burnore

      #3
      Re: Setting up a page that will intercept all pages that do not already exist?

      On Tue, 07 Dec 2004 21:29:00 GMT, "Joshua Beall"
      <jbeall@donotsp am.remove.me.he raldic.us> wrote:
      [color=blue]
      >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![/color]


      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@databa six 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
      =============== =============== =============== =============== ===============

      Comment

      • Joshua Beall

        #4
        Re: Setting up a page that will intercept all pages that do not already exist?

        "Daniel Tryba" <spam@tryba.inv alid> wrote in message
        news:41b622af$0 $33205$e4fe514c @dreader3.news. xs4all.nl...[color=blue]
        > In comp.lang.php Joshua Beall <jbeall@donotsp am.remove.me.he raldic.us>
        > wrote:[color=green]
        >> 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.[/color]
        >
        > 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...[/color]

        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?


        Comment

        • Joshua Beall

          #5
          Re: Setting up a page that will intercept all pages that do not already exist?

          >>This is in a LAMP environment, and does not need to be portable to Windows[color=blue][color=green]
          >>or any other webserver.[/color]
          >
          > 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.[/color]

          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?


          Comment

          • paul

            #6
            Re: Setting up a page that will intercept all pages that do not alreadyexist?

            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:[color=blue]
            > 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
            >
            >[/color]

            Comment

            • Daniel Tryba

              #7
              Re: Setting up a page that will intercept all pages that do not already exist?

              Joshua Beall <jbeall@donotsp am.remove.me.he raldic.us> wrote:
              [404 handler][color=blue]
              > Ah ok.
              >
              > Are there other ways to do it? What are they? Do they provide advantages
              > of some sort?[/color]

              You could use the rewriteengine to redirect all requests to 1 script.

              [color=blue]
              > Is there any reason to not go the 404 handler route?[/color]

              Can't think of any.

              Comment

              • Rob

                #8
                Re: Setting up a page that will intercept all pages that do not already exist?


                "Joshua Beall" <jbeall@donotsp am.remove.me.he raldic.us> schreef in bericht
                news:wgptd.1810 $N%6.99@trnddc0 5...[color=blue]
                > 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
                >[/color]

                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


                Comment

                • d

                  #9
                  Re: Setting up a page that will intercept all pages that do not already exist?

                  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.



                  d.

                  ok, two hints - "RewriteCon d" and "-f" :)

                  "Joshua Beall" <jbeall@donotsp am.remove.me.he raldic.us> wrote in message
                  news:wgptd.1810 $N%6.99@trnddc0 5...[color=blue]
                  > 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
                  >[/color]


                  Comment

                  • usenet@isotopeREEMOOVEmedia.com

                    #10
                    Re: Setting up a page that will intercept all pages that do not already exist?

                    On Wed, 08 Dec 2004 08:55:22 GMT, "Rob" <reply_@news_gr oup.please> wrote:
                    [color=blue]
                    >
                    >"Joshua Beall" <jbeall@donotsp am.remove.me.he raldic.us> schreef in bericht
                    >news:wgptd.181 0$N%6.99@trnddc 05...[color=green]
                    >> 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
                    >>[/color]
                    >
                    >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",
                    >ErrorDocumen t 404 http://mysite.com/pages/display.php
                    >
                    >
                    >HTH Rob
                    >[/color]

                    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 !

                    Comment

                    • Andrew

                      #11
                      Re: Setting up a page that will intercept all pages that do not already exist?

                      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.

                      Comment

                      • Michael Fesser

                        #12
                        Re: Setting up a page that will intercept all pages that do not already exist?

                        .oO(Andrew)
                        [color=blue]
                        >Using the ErrorDocument 404 syntax to redirect everything not found to
                        >a PHP script will issue a 404 header to the browser,[/color]

                        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).
                        [color=blue]
                        >but none of the
                        >browsers I have tried, Opera, Firefox or IE show an error page provided
                        >the header is followed by html.[/color]

                        They don't show an error because they don't receive an error code.

                        Micha

                        Comment

                        • Gordon Burditt

                          #13
                          Re: Setting up a page that will intercept all pages that do not already exist?

                          >Using the ErrorDocument 404 syntax to redirect everything not found to[color=blue]
                          >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.[/color]

                          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_STATU S'] 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_REQUE ST_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

                          Comment

                          • Helen Page

                            #14
                            Re: Setting up a page that will intercept all pages that do not alreadyexist?

                            Joshua Beall wrote:[color=blue]
                            > 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![/color]

                            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.

                            Comment

                            • Andrew

                              #15
                              Re: Setting up a page that will intercept all pages that do not already exist?

                              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_que ries = explode('&', $_SERVER['REDIRECT_QUERY _STRING']);

                              foreach ($redirected_qu eries as $redirected_que ry)
                              {
                              $redirected_que ry = explode('=', $redirected_que ry, 2);
                              $_GET[urldecode($redi rected_query[0])] =
                              urldecode($redi rected_query[1]);
                              }

                              unset($redirect ed_queries, $redirected_que ry);
                              }

                              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.

                              Comment

                              Working...