Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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;


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


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

Any help will be appreciated.


share|improve this question

So I finally got it working:

location ~ ^/css/profiles/(?!g/)(.*)$ {
  set $new_uri /css/profiles/g/$1;
  if (!-f $request_filename) {
    rewrite ^/css/profiles/(.*)$ /css/profiles/g/$1 last;

...and at the end:

location ~ \.php$ {
  fastcgi_split_path_info ^(.+\.php)(/.+)$;
  fastcgi_index index.php;
  include fastcgi_params;

  ... some other stuff were here related to fastcgi
  fastcgi_param PATH_INFO $new_uri; # <--- I added this
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.