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

$_SERVER[REQUEST_URI] + 404Error

watagal
P: 3
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
Jan 23 '09 #1
Share this Question
Share on Google+
5 Replies


Atli
Expert 5K+
P: 5,058
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.
Jan 23 '09 #2

watagal
P: 3
Thanks for the quick reply.

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

Expand|Select|Wrap|Line Numbers
  1. 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
Jan 23 '09 #3

Atli
Expert 5K+
P: 5,058
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
Expand|Select|Wrap|Line Numbers
  1. ErrorDocument 404 /mysite.com/code_php/custom404page.php
And then use either the REQUEST_URI or the REDIRECT_URL.
Jan 23 '09 #4

watagal
P: 3
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!
Jan 23 '09 #5

Atli
Expert 5K+
P: 5,058
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 :]
Jan 23 '09 #6

Post your reply

Sign in to post your reply or Sign up for a free account.