.htaccess Redirect to External Site and Forward POST data while Changing address bar?

放肆的年华 提交于 2019-12-11 10:13:32

问题


Hey, I want to use .htaccess to redirect the requested page to the exact same page on a different domain, and I want it to forward all POST data while CHANGING the address bar to the new domain, like a normal redirect.

Here's my code.

Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) http://www.domain1.com/$1 [R=301,L] 

The problem is that POST data is not sent this way. I then tried this:

Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) http://www.domain1.com/$1 [R=301,P]

And that works to forward POST data, however the address bar does not change to the new domain1.com

How can I accomplish this?

Thanks!


回答1:


Unfortunately, the HTTP protocol does not support this.

When executing your first example, the web server sends the requesting browser a "Location" header telling the browser to navigate to a specified URL. The browser attempts to load this new URL like an ordinary web page, including displaying the URL in the browser's address bar.

Since the browser is the system loading the new URL, the browser must be the system to re-POST the submitted data. The web server can't do it. Unfortunately, the HTTP protocol does not provide a way for the web server to tell the browser to preform this re-POSTing.

Is there an alternate way to achieve your goal?




回答2:


I think that's not posible because to change URL in browser, HTTP server must return some response which will tell browser to send POST data to new URL. Which as far as I know is not posible without using javascript. When you've specified [P] option HTTP server is acting like a proxy server, hiding real source of data from browser. I see 2 posible solutions:

  • use GET instead of POST
  • or create some script which runs on HTTP server and make POST redirects

UPD: You can create PHP script which will be a handler for you redirect rules. The script should create following html code:

<body onload="document.forms[0].submit()">
<form action="post" action="<?=$your_new_url_here>">
<?php
    foreach ($_POST as $param => $value):
?>
<input type="hidden" 
    name="<?=htmlspecialchars($param)?>" 
    value="<?=htmlspecialchars($value)?>" />
<?php
    endforeach;
?>
</form>
</body>


来源:https://stackoverflow.com/questions/1562895/htaccess-redirect-to-external-site-and-forward-post-data-while-changing-address

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!