views:

3222

answers:

4

Hi Stackers

I have a website where all requests are redirected silently (via .htaccess) to index.php and then PHP is used to show the correct page (by traversing the REQUEST_URI).

I was wondering if it's possible to submit POST data to a fake address too?

I've currently got my form like so

<form action="/whatplant/send-mail" method="post" id="contact-us-form">

And my .htaccess rule is

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

My index.php checks isset($_GET['send-mail']) which works fine.

This however seems to drop off all the POST data that should be sent to it.

Is there a way to keep the post data? I don't want to use GET because it can't send as much information, though it might not be an issue with a simple enquiry form.

Thank you!

EDIT: I changed the form's action to a test.php with print_r($_POST) and got the correct output. I am still having problems receiving the post data when the action is sent to index.php

Here is my .htaccess for redirecting to index.php

# serve files and dirs if they exist please, otherwise send to index
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule . index.php
+4  A: 

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).

Tautologistics
Note that you need to enable the proxy module, and the proxy_http_module in the config files for this to work. Also, you might need to set the absolute path to the rewrite rule. That is, in the example above, you would use `RewriteRule send-mail /path/to/index/index.php?send-mail [NC,P]` (or at the very least a `/` before index)
Marius
+1  A: 

As long as you are only using an internal rewrite, not an HTTP redirect, you should not lose POST data. Here is the rule I use on my site:

RewriteRule ^(.*)$ index.php/$1 [L]

Try using the HTTPLiveHeaders extension for Firefox (or something similar) and track the entire page request. Make sure you are not getting an HTTP redirect. If you get a HTTP/1.1 3xx response and Location: http://address header, that is the problem. Your rewrite rule that you posted should not cause that to happen. If you are being redirected, there is probably either an error in your PHP code or another rewrite rule that is being applied.

mcrumley
Nope I'm getting back 200 OK :S
alex
+1  A: 

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.

Bill Karwin
Theres a small limitation in that, once you hit some arbitrary limit ( i think its 1024 characters ), data following it gets stripped from the request. Especially problematic if it happens to be a user input field.
Kent Fredric
(NB: Limit is server *and* browser specific, RFC says its unbounded, but we know how often some companys read that )
Kent Fredric
+1 I'm a bit more enlightened since I asked this question and now fully understand all modification of data should be POST
alex
Awesome! It's those "aha!" moments that really count. :-)
Bill Karwin
A: 

I don't have enough points to reply to Bill directly but his solution will not work. With "client side" redirects, browsers do not maintain POST data in subsequent requests. Only "server side" redirects have any chance of keeping the POST data intact.

Tautologistics