4

Is there any to to capture query string and append it to error document (404). Something like:

Requested page:

http://www.example.com/sdsdsd

Redirect to:

http://www.example.com/404.php?pg-name=sdsdsd
MrWhite
  • 43,179
  • 8
  • 60
  • 84

1 Answers1

0

Instead of using ErrorDocument in the htaccess configuration, you could force a real redirect to the error.php like this:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /error.php?pg-name=%{REQUEST_FILENAME} [L]

Source: How to get `$_POST` query when 404 redirected through .htaccess?

In the error.php page you can then check, if the pg-name parameter is given to the error.php:

echo '<pre>';
print_r($_GET);
echo '</pre>';

If you call you page like this: http://domain1.com/asdasdasd (assuming the link is not found), the output from the error.php will look like this:

Array
( 
    [pg-name] => /path_to/public_html/asdasdasd
)

Then you can get to the requested string "asdasdasd".

Community
  • 1
  • 1
meberhard
  • 1,797
  • 1
  • 19
  • 24
  • I am using this RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.+)$ /404.php?pg-name=$1 [L,R] but the problem is that it is not setting header to 404 rather setting the header to 302 – Rameez Jawaid May 14 '14 at 12:03
  • "force a real redirect" - Note that this is an _internal rewrite_, not an _external "redirect"_. This is good, you don't want to "redirect" (`R` flag on the `RewriteRule`) the request, otherwise the user will be sent a 3xx status. However, you will need to manually set the appropriate 404 HTTP status code in `error.php`. – MrWhite Oct 24 '16 at 19:10