How to extract URL parameters on 404 Error in PHP

2019-09-04 15:18发布

问题:

I have my htaccess redirecting to a error404.php but I want that PHP file to be able to extract the original url parameters.

For example:

http://mywebsite.com/unknownfile.php?param1=value1&param2=value2

I've tried $_GET['param1'] but that's empty. The $_SERVER['PHP_SELF'] just shows error404.php

Many thanks in advance.

回答1:

You can access original URL via $_SERVER["REQUEST_URI"].

For example: requesting URL /hihi/meow?key=rumba which does not exists. The $_SERVER["REQUEST_URI"] will have that string, which you can parse with parse_url() function to split into parts and use other functions (like explode() to get to individual query strung parameters.



回答2:

If you redirect this info will only be present in $_SERVER['HTTP_REFERER']