Is it possible to redirect post data?

Solution 1:

Try this:

# redirect mail posting to index
     RewriteRule send-mail index.php?send-mail [NC,P]

"P" acts like "L" in that it stops processing rules but it also tells the module that the request should be passed off to the proxy module intact (meaning POST data is preserved).

Solution 2:

You should be able to simply redirect to index.php, and then in that script, access $_SERVER['REQUEST_URI'] to see the original request, with "send-mail" intact.

By the way, "can't send as much information" is not the reason to use POST. The reason to use POST is that the request will modify data on your site, instead of simply retrieving data.

Suppose you put a hyperlink on your page with a GET request like "/delete_user?id=1234," and then some search engine innocently follows the link as it's indexing your site. That's why GET requests are not good for requests that modify data.

Solution 3:

I've found the most reliable way is to use the 307 status code.

RewriteRule send-mail index.php?send-mail [R=307,L]

Status code 307 indicates that the request should be repeated with the same HTTP method and data. So your POST request will be repeated along with its data if you use this status code.

You may detect if the request is a POST request as well in case you need to support standard 301 redirects for non POST requests.

# POST requests.
RewriteCond %{REQUEST_METHOD} POST
RewriteRule send-mail index.php?send-mail [R=307,L]

# Standard GET requests.
RewriteRule send-mail index.php?send-mail [R=301,L]

Solution 4:

To avoid issues with some proxies and Apache rewrites, pass in POST data or set the Content-Length: 0 header for requests with an empty body.

I recently had issues with Apache converting my request to a GET when doing a POST with an empty body. So, instead of this:

 curl -X POST https://example.com/api/user/123456789

pass the Content-Length header:

 curl -X POST https://example.com/api/user/123456789 -H 'Content-Length: 0'

or pass something in the body:

 curl -X POST https://example.com/api/user/123456789 -d ''