6

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

Community
  • 1
  • 1
Nok Imchen
  • 2,802
  • 7
  • 33
  • 59
  • 1
    It's not a "trick", it is just a terrible solution. Why not using `mod_rewrite`? ps: no, after 404 redirect you've already lost post data and you cannot get it – zerkms May 01 '12 at 11:26
  • not sure, but I think that you will loose all POST data when redirecting. what do you need it for? maybe there is some different way – miro May 01 '12 at 11:28
  • Please note that there is no such thing as a `404 redirect`. 4xx response codes indicate a client error (404 indicates that the resource requested by the client does not exist on the server), 3xx codes are for redirection. If you want to force a client to resubmit and identical POST request to a new URI, you should use a 307 status code. But I suspect that what you actually need to use here is `RewriteCond %{REQUEST_FILENAME} !-f` and `RewriteCond %{REQUEST_FILENAME} !-d` to rewrite requests for files that don't exist to a specific script. – DaveRandom May 01 '12 at 11:49
  • I'd like to view the contents of the index.php with dynamic contents depending upon the post variable when the user request for a page that doent exist (without changing the url in the url address bar) – Nok Imchen May 01 '12 at 11:50
  • 1
    The user should not be sending a post request unless they have submitted a form, and all forms on your site should point to a resource that exists. mod_rewrite is going to be what you want here. – DaveRandom May 01 '12 at 11:53
  • @DaveRandom, zerkms, MireSVK Thnaks a lot to all of you. I got my problem solved :) – Nok Imchen May 01 '12 at 12:14
  • @DaveRandom could you put your solution into a complete answer? I'm having a hard time determining how to use these. – David May 16 '13 at 07:11

3 Answers3

7

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]

SHA2NK
  • 189
  • 2
  • 12
6

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

Example:

FallbackResource /404.php
user2816761
  • 81
  • 1
  • 1
  • This is the solution that worked for what I was needing. Thank you for providing it. One important note for those implementing this solution: You will have to specify the actual file by name. For example, in my project I previously had "ErrorDocument 403 /router/" as my directive without specifying "index.php" at the end. When I tried "FallbackResource /router/" it failed until I changed it to "FallbackResource /router/index.php". – Art Geigel Aug 31 '17 at 19:52
  • 2
    See also this post which should save you an hour's worth of time trying to debug why it doesn't work for the root endpoint of "/": https://serverfault.com/questions/512735/fallbackresource-directive-works-for-any-uri-except – Art Geigel Aug 31 '17 at 20:39
5

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.

hakre
  • 193,403
  • 52
  • 435
  • 836
  • Thanks, i av solved the problem with the help of DaveRandom's idea :) Anways, thanks to you too for helping :) – Nok Imchen May 01 '12 at 12:12
  • This is the simplest way to go if you don't need to get the post data. – Jack Franzen Mar 27 '15 at 16:16
  • 2
    @JackFranzen: Oh, there is also the [**`FallbackResource`** Directive](https://httpd.apache.org/docs/current/mod/mod_dir.html#fallbackresource) which doesn't have the POST limitations as discussed in this answer. See as well http://stackoverflow.com/a/8196767/367456 and http://stackoverflow.com/a/7968869/367456 where I covered it earlier. – hakre Mar 27 '15 at 17:53