如何preserve POST数据通过AJAX请求后的.htaccess重定向?重定向、数据、POST、preserve

2023-09-02 11:25:36 作者:你眼有多红

.htacesss

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

例如AJAX URL请求: http://hostname.com/api/ext/list.php?query=de

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

我希望能够重定向的URL在此格式下 ?的index.php API = {requested_filename}&放大器;参数1 =值1&放大器;参数2 =值2 ......

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

因为整个网站是通过引导过程中的index.php具有路由部分装载的configs,处理模板等...

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

当我尝试一个jQuery code为例,POST数据重定向后丢失。

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

我读过,你不能在一个HTTP重定向preserve数据。但是,如何所有的框架避免?我有codeD的多,而每个人都通过index.php文件bootstraped并有在.htaccess文件中启用pretty的网址重写规则。因此,在Yii中,例如,我会叫网址API /上传/ latests.json一些POST数据并在后端的控制器将收到的数据。缺少什么我在这里?

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?

请注意:我测试过的 [P] mod_rewrite的参数,我认为这台服务器没有启用的mod_proxy

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

推荐答案

有一个重写和重定向之间的差异。

There is a difference between a rewrite and a redirect.

重写是一个Apache(以及其它服务器)模块,将遵循一套电导率/规则来映射请求的URL到服务器上的文件(例如:自举把所有的URL到一个文件,通常的index.php。一个MVC可能映射/模型/控制器/视图URI来调用相应的MVC文件的index.php文件)。

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.php并且该网页说:你正在寻找的是B.php等你的浏览器去B.php。

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.

一个重写将preserve后的参数,因为URL不会改变。重写只会改剧本被请求,但对浏览器,它看起来像页面仍然存在于请求的URL。

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.

一个重定向不会preserve后的参数,因为该服务器将您重定向到另一个页面完全。

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.

要解决这个问题,你是如何在index.php文件检查有没有交参数?你确定你期望的控制器获取调用?

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?