Hi – I’ve been struggling with this for days. It seems simple but I just can’t get it done.
I have a site developed in CakePHP. There’s a script that responds to /css/profiles/g/whatever.css (“whatever” being whatever, it is actually a parameter that gets passed to the action), it echoes a generated CSS and saves it to /css/profiles/whatever.css.
I have a rule in Apache that takes requests to /css/profiles/whatever.css and, if it doesn’t exist, rewrites the request to /css/profiles/g/whatever.css without redirecting, so the client never notices it was responded by a script and that the file didn’t exist.
This is what I have in Apache:
# Profile CSS rules
RewriteCond %{REQUEST_URI} ^/css/profiles/
RewriteCond %{REQUEST_URI} !/css/profiles/g/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^css/profiles/(.*)$ /css/profiles/g/$1 [L]
# CakePHP's default rules
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/$1 [QSA,L]
Now I’m moving the site to a server with Nginx, and so far I’ve got this:
# Profile CSS rules
location ~ ^/css/profiles/(?!g/)(.*)$ {
if (!-f $request_filename) {
rewrite ^/css/profiles/(.*)$ /css/profiles/g/$1 last;
break;
}
}
# CakePHP's default rules
location / {
try_files $uri $uri/ /index.php?$uri&$args;
}
The conditions seem to be working, because if I go to /css/profiles/whatever.css and print out PHP’s $_SERVER variable it gives me
[QUERY_STRING] => /css/profiles/g/whatever.css&
Notice the &. It means it got to the try_files part and added the $uri to the query string, and it has the correct $uri.
But…
[REQUEST_URI] => /css/profiles/whatever.css
That’s the glitch. It seems it’s not really changing the $request_uri which is what CakePHP needs to control what controller attends what.
Update: The REQUEST_URI value is right… the issue here is that Cake looks for the value of different server variables to decide which controller will respond. In this order: $_SERVER['PATH_INFO'], $_SERVER['REQUEST_URI'], a combination of $_SERVER['PHP_SELF'] and $_SERVER['SCRIPT_NAME'] and finally $_SERVER['HTTP_X_REWRITE_URL']. That’s why it’s failing.
Any help will be appreciated.
Thanks.
Note: I posted this question on Serverfult yesterday because I think it fits better there but didn’t get an answer, that’s why I’m posting it here too.
So I finally got it working:
…and at the end: