Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...