$_SERVER[REQUEST_URI] + 404Error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • watagal
    New Member
    • Jan 2009
    • 3

    $_SERVER[REQUEST_URI] + 404Error

    Greetings

    I'm writing an error handling page for the common "404 Page Not Found" error using PHP.

    I need to retrieve the actual URL that was typed by the user.

    When I use $_SERVER['REQUEST_URI'] - it returns the path for the current running PHP script.

    Is there a way to intercept the URI entered by the user?

    TIA, Watagal
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Hi.

    That depends on how your user is redirected to your error handling page.

    In some cases I imagine the $_SERVER['HTTP_REFERER'] could be used.
    In other cases you may need to have your HTTP server forward the actual URL to the error page URL.

    Comment

    • watagal
      New Member
      • Jan 2009
      • 3

      #3
      Thanks for the quick reply.

      I'm using Apache 2.0 html server, where the .htaccess file redirects via:

      Code:
      ErrorDocument 404 http://localhost/mysite.com/code_php/custom404page.php
      I tried using "$_SERVER['HTTP_REFERER']" and it returns empty.

      Thanks again and if you think of anything else, plz let me know.
      Watagal

      Comment

      • Atli
        Recognized Expert Expert
        • Nov 2006
        • 5062

        #4
        Added some info

        Hmm, ok.

        Try the $_SERVER['REDIRECT_URL'].
        That should give you the path of the invalid URL relative to your web-root.

        Edit
        Although, the absolute URL you use might upset that.
        Try changing the .htaccess to just
        Code:
        ErrorDocument 404 /mysite.com/code_php/custom404page.php
        And then use either the REQUEST_URI or the REDIRECT_URL.

        Comment

        • watagal
          New Member
          • Jan 2009
          • 3

          #5
          Thanks Atli!

          Changing the .htaccess file to the relative path worked for me. For others following this thread, either REQUEST_URI or REDIRECT_URL worked fine - not sure of the difference (yet).

          Thanks again!

          Comment

          • Atli
            Recognized Expert Expert
            • Nov 2006
            • 5062

            #6
            Glad you got it working :)

            As far as I know, the REDIRECT_URL variable is set by Apache only after these sort of redirects. (And it only seems to do so for internal redirects, which is why you had to remove the absolute URL).

            The REQUEST_URI variable, however, is always set to the path of the URL relative to the web-root. In cases like these, when you use an absolute URL in a ErrorDocument redirect, like your original .htaccess did, Apache redirects you "the old fashion way", which also changes the REQUEST_URI.

            But in the case it doesn't have to redirect outside it's own web-root (if you give it a relative path, that is), it doesn't seem to do that, but simply serves a different file without changing the actual URI, which allows us to fetch the original URL from the REQUEST_URI variable.

            That is at least how it looks based on my experiments with this :]

            Comment

            Working...