.htaccess Redirect to External Site and Forward POST data while Changing address bar?
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!
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?
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>
精彩评论