0

Context: I have Cakephp v2.3 and I'm running XAMPP. My directory structure is thus:

C:\xampp\htdocs\cakephp\app\webroot

I have a .htaccess file located in ..\xampp\htdocs that redirects to cakephp (so basically I can type localhost and it will redirect me to localhost/cakephp)

This could be the root of my issue, but its the only way I was able to get cake to work.

Given this, everything was working fine UNTIL... Posting with Ajax over SSL.

Problem: The Ajax request originates from an https page and posts to another https page (so it's not a domain issue). The manifestation of the problem is the Post changing to a "Get" and dropping the posted data. If I make the request a "Get" it works fine (but I don't want to do that because it's posting data in the truest sense). If I make both actions use http it works. I believe this redirect is occurring because Apache is re-writing "/action" to "/cakephp/action"

What's Confusing: I have debugs in the CakePhp before filter and when I put a return in that method (the beforeFilter) my Client side Developer Tools say the request was Post, all the data was present, and the server response is my debug line. When I put the return as the first line in the target action the problem manifests (IE: the client tools show the request as being a Get with no data and the initiator is the target action instead of a jQuery command).

This confuses me because once it gets into the beforeFilter I would ASSUME Apache no longer redirects it. And I also assume, per the CakeBook Documentation, that the next method called after beforeFilter completes is the target Action. But for some reason, between the beforeFilter completing and the targetAction starting the request is redirected to /cakephp/action

Relevant Code:

.htaccess at ..\xampp\htdocs

<IfModule mod_rewrite.c>
   RewriteEngine on
   RewriteRule    ^$ cakephp/app/webroot/    [L]
   RewriteRule    (.*) cakephp/app/webroot/$1 [L]
</IfModule>

Thanks in advance to any brave helpers!

isick
  • 1,467
  • 1
  • 12
  • 23
  • `mod_rewrite` is out of the game once the request reached the app code, so I would be very surprised if it would cause that redirect. Components do run between `beforeFilter()` and the controller action, so you might want to check whether one of them is issuing a redirect. Also I'd suggest to change your setup, do not install XAMPP in the windows folder, and ditch the `.htaccess` in `htdocs`, instead access the app including the subdirectory in the path (this should work out of the box), or even better use virtual hosts. – ndm Oct 31 '13 at 19:09
  • Sorry, Xampp isn't in the Windows folder (my mistake). I would just use the directory path in the URL, but that would mess up my URL patterns between Production and Dev environments since XAMPP is only on my local set up. And adding additional routes is not what I want to do. I'd rather make Dev bend to match production than vice versa. I'll look into Virtual Hosts. I'm not super familiar with them, but it might be a solution. Thanks! – isick Oct 31 '13 at 21:01

1 Answers1

0

After some time and playing around (and modifying other pieces of code) I've discovered the root of the issue.

Xampp was redirecting, but the real issue was cakePhp's CSRF token validation. Because my request where AJAX (and I wasn't serializing a generated form) there was no csrf token.

This was resolved by adding

$this->Security->csrfCheck = false;
$this->Security->validatePost = false;

In my Controller. Clearly I want those requests validated, but that's how I was able to determine the actual problem.

Cake should probably do a better job of making this case known, but anyway that's the solution if anyone else encounters a similar situation.

Best of luck!

isick
  • 1,467
  • 1
  • 12
  • 23