Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to preserve POST data via ajax request after a .htaccess redirect?

.htacesss

RewriteCond %{REQUEST_URI} ^/api/(.+)$
RewriteRule ^api/(.+)$ /index.php?api=%1 [QSA,L]

example ajax url request: 'http://hostname.com/api/ext/list.php?query=de'

I want to be able to redirect urls in this format to the following index.php?api={requested_filename}&param1=value1&param2=value2 ...

because the whole site is processed through a bootstrap process in index.php which has a routing part loading configs, templates etc...

When I try a jquery code for example, the POST data is lost after redirect.

$.ajax({
            url: '/api/contact.php',
            type: 'POST',
            data: { 
                email: $("#contactEmail").val(),
                name: $("#contactName").val(),
                message: $("#contactMessage").val()
                                // etc ...
            }
});

I've read that you cannot preserve data on a http redirect. But how do all the frameworks avoid that? I've coded in many, and every one is bootstraped through the index.php and there are rewrite rules in the .htaccess file for enabling pretty urls. So in Yii for example, I would call an url "api/uploads/latests.json" with some POST data and the controllers on the backend would receive that data. What am i missing here?

note: I've tested the [P] mod_rewrite parameter, and i think that this server doesn't have mod_proxy enabled.

like image 525
Keeper Hood Avatar asked Dec 14 '12 21:12

Keeper Hood


2 Answers

There is a difference between a rewrite and a redirect.

Rewrite is an apache (and other servers) module that will follow a set of cond/rules to map a requested url to files on the server (ex: a bootstrap rewrites all urls to a single file, usually index.php. A mvc might map /model/controller/view uri to an index.php that calls the appropriate mvc files).

A redirect actually changes the page you are on. Someone requests page A.php and that page says "what you are looking for is on B.php" and so your browser goes to B.php.

A rewrite will preserve post parameters because the url doesn't change. A rewrite will just change the script being requested, but to the browser it looks like the page still exists at the requested url.

A redirect will not preserve post parameters because the server will redirect you to another page completely.

What it appears you are trying to do is a rewrite, not a redirect. You should have no problems getting the post parameters.

To fix this, how are you checking in index.php that there are no post parameters? Are you sure the controller you are expecting is getting called?

like image 84
Jonathan Kuhn Avatar answered Nov 14 '22 23:11

Jonathan Kuhn


All POST data is lost on redirect. There is no way to preserve it via htaccess rewrite/redirect rules.

The redirect (all 301,302,303) received by the client (all major browsers I know) is treated as a new url to make a GET request to. Browsers won't automatically tack on old post parameters to this URL--even if the source of the redirect was a POST request.

The only way I've every found around this is to do the rewrite inside code and covert the POST parametes to GET parameters and stick them on the end of the new url. In php you then issue a header location change (or whatever redirect call your library of choice uses):

header("Location: http://www.example.com/?my_old_post_args=123"); 
exit;
like image 32
Ray Avatar answered Nov 14 '22 21:11

Ray