0

I have an application built with CodeIgniter that a 3rd party service (an email service provider) is making http requests containing POST data to when certain events occur. The problem is, the url that this 3rd party service is making the requests to is not fully modifiable by me, specifically, it make its requests to a file named w_4.php at the domain and directory that I specify.

Instead of having this separate file outside of the CodeIgnitier stack, I would like to, using .htaccess, rewrite all the requests made for this file to a controller within my codeigniter application, i.e. api/my_controller/my_method. But I need the POST data in the request intact, so I would think a redirect could not be used.

I tried something like this in my .htaccess:

RewriteCond $1 (w_4\.php)
RewriteRule ^(.*)$ index.php/api/my_controller/my_method

While the request does get routed to codeigniter, I just get a 404 even though that controller/route does exist.

Bill Dami
  • 3,205
  • 5
  • 51
  • 70
  • With `[R,L]` flags the rule works fine, it redirects to mydomain.com/index.php/api/my_controller/my_method and it calls the `my_method` method in the `my_controller` controller. It seems that maybe even though the URL is being rewritten, codeigniter is still just looking at the actual passed URI of "w_4.php" to figure out the route and therefore reporting a 404? – Bill Dami Mar 28 '13 at 18:25
  • I am a bit confused, so the redirect works, `my_method` does its things, but you get a 404? – Marc Audet Mar 28 '13 at 18:29
  • w/o the redirect flag `[R]` (just internally rewriting the request) I get a 404, but with the redirect my_method is called successfully. However doing the rewrite rule as a redirect would not be the way to go in this case I would think, because the original request contains POST data that I need to be able to access in my_method. – Bill Dami Mar 28 '13 at 18:30
  • @Bill If it is a query, it will be appended automatically to the substitution URL so that shouldn't be a problem. As I said in my previous comment, add the [R,L] flags to see the substitution URL to make sure the POST data is also passed. – Felipe Alameda A Mar 28 '13 at 18:45
  • @faa @Marc - I got it working. In order to keep the POST data preserved all that was needed is `[P]` instead of `[R,L]`. the `P` flag indicates that the request should be passed off to the proxy module completely intact as I found out [here]( http://stackoverflow.com/questions/358263/htaccess-is-it-possible-to-redirect-post-data), whereas just using the `R` redirect flag causes the POST data to be lost (ran a test to verify this). – Bill Dami Mar 28 '13 at 19:01
  • @Bill Good. Glad you solved the problem. – Felipe Alameda A Mar 28 '13 at 19:03
  • @BillDami So `[P]` does the trick, that is very good to know. – Marc Audet Mar 28 '13 at 19:04

1 Answers1

1

The answer is to use the P proxy flag in the RewriteRule. As detailed here https://stackoverflow.com/a/359224/192694, the flag indicates that the request should be passed off to the proxy module intact, including its POST data.

For more info: http://httpd.apache.org/docs/2.2/rewrite/flags.html#flag_p

Community
  • 1
  • 1
Bill Dami
  • 3,205
  • 5
  • 51
  • 70