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