How to get the `$ _POST` request when 404 is redirected via .htaccess?

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

Well, I have an index.php file that handles all 404 redirects .

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

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

ErrorDocument 404 / index.php

+6
source share
3 answers

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

I solved the 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]

+7
source

With the following directive:

 ErrorDocument 404 /index.php 

The Apache web server internally redirects to a new location. Internally, the client (browser) will not change the URL in the address bar, because the redirect is not transmitted to the browser.

Since this is a redirect, the POST request turns into a GET request.

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

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

In short, you cannot use the ErrorDocument directive to rewrite URLs for HTTP POST requests.

To do this, you need to use the mod_rewrite module or create your own apache handler.

+5
source

Use the FallbackResource directive ErrorDocument Apache ErrorDocument directive: this does the FallbackResource trick on the Apache website

Example:

 FallbackResource /404.php 
+2
source

Source: https://habr.com/ru/post/914620/


All Articles