简体   繁体   中英

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

This question is somewhat related to my previous question . The trick of using $_SERVER['REDIRECT_QUERY_STRING'] seems to work only for $_GET variables.

Well, I've an index.php file which handles all the 404 redirects .

If a user requests for a page which doesnt exist, say, apple.php?item=23 , then using $_SERVER['REDIRECT_QUERY_STRING'] I can get the $_GET variable item=23 , but if the variable is not $_GET but $_POST then $_SERVER['REDIRECT_QUERY_STRING'] doesn't work.

How can I get $_POST variable when I redirect it to index.php using the following .htaccess setting

ErrorDocument 404 /index.php

Check answer here : http://www.brainonfire.net/blog/apache-pitfall-errordocument-post/

I solved my problem using this.


OR put this in your .htaccess file :

RewriteEngine On
    RewriteBase /


    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /yourErrorDocument.php [L]

With the following directive:

ErrorDocument 404 /index.php

the apache webserver does an internal redirect to the new location. Internal means, the client (browser) won't change the URL in it's address bar because that redirect is not communicated to the browser.

Because it's an redirect, the POST request is turned into a GET request.

You can see this by looking into the following two $_SERVER variables:

$_SERVER['REDIRECT_REQUEST_METHOD'] # POST
$_SERVER['REQUEST_METHOD'] # GET

So in short, you can not use the ErrorDocument directive to do URL rewriting for HTTP POST requests.

You need to use the mod_rewrite module for this or create your own apache handler.

Use the FallbackResource directive instead of the ErrorDocument directive of Apache: it does the trick FallbackResource on Apache website

Example:

FallbackResource /404.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM