2

Stackoverflow users,

I have an Apache application that needs to accept data POSTed to the following paths:

/sample/HostChange/Submit
/sample/HostChange/SubmittoAPI

I'm currently using the following 301 redirect rules. This is not what I want as as the POST gets redirected and the second request is a GET loosing all the data. I am seeing the 301 request go the correct url but the second request is a GET and causes a 405 response code.

.htaccess:

RewriteEngine On
Redirect 301 /sample/HostChange/Submit /event
Redirect 301 /sample/HostChange/SubmittoAPI /date

I'm sure using a Redirect is the issue. Can someone help me figure out the correct RewriteCondition I need to be using to redirect these POST hits to the new paths but keep the data being submitted to the application.

Thank you mucho.

god
  • 65
  • 2
  • 8
  • ¿Have you tried using mod-rewrite? The reason I ask is in the Redirect (httpd.apache.org/docs/2.2/mod/mod_alias.html) directive `"The old URL-path is a case-sensitive (%-decoded) path..."` and that might be modifying the POST, which in turn could affect the next GET. Just an idea, but if that is the case, you should try using mod-rewrite instead. – Felipe Alameda A Dec 21 '12 at 03:40
  • Unless I am missing something, you are using Redirect, which is a [mod-alias](http://httpd.apache.org/docs/2.2/mod/mod_alias.html) directive. I meant RewriteRule which is a [mod-rewrite](http://httpd.apache.org/docs/current/mod/mod_rewrite.html) directive. – Felipe Alameda A Dec 21 '12 at 05:16
  • Oh yes. My mistake. I need to be using mod_redirect to do the work. Do you have any guidance on RewriteCond/Rule? – god Dec 21 '12 at 07:14
  • 1
    See http://stackoverflow.com/questions/10586779/redirectmatch-changes-post-to-get/10624195#10624195 – Gerben Dec 21 '12 at 21:37

3 Answers3

0

I don't think you can redirect a POST (as a POST). Browsers just won't POST the data again.

You'd have to output some HTML with Javascript to make the browser rePOST the data to the new URL.

Or instead of redirecting, have some server-side code accept the POST data first and then dispatch it somehow (maybe be redirecting with an indentifying token in the URL) internally.

Or if the data is short, re-write the URL to include the data as query parameters.

Thilo
  • 257,207
  • 101
  • 511
  • 656
0

Something along the lines of following should work, I'm not around an apache instance at the moment so regex is not tried but please check the rewrite log and see how it behaves.

RewriteEngine On
RewriteLog /var/log/httpd/rewrite.log
RewriteLogLevel 9
RewriteCond %{REQUEST_URI} /sample/HostChange/Submit
RewriteRule ^/sample/HostChange/Submit(.*) /event/$1 [P,L]
opensourcegeek
  • 5,552
  • 7
  • 43
  • 64
  • Can you please show what apache is writing to rewrite log for sinle request to /sample/HostChange/Submit? As I said I haven't checked the regex, but the above structure will preserve your payload if any, also the query params if any in request to the original URL. I use something similar in production to redirect request to zope or jboss depending on URL pattern. – opensourcegeek Dec 22 '12 at 07:27
0

I some kind of the same problem, for me it helped not to really redirect the request, but to rewrite it. I don't now if this is applicable to your problem.

Here are the details and it worked for me: PHP Rewrite url and preserve posted data

Community
  • 1
  • 1
Seb
  • 101
  • 6