Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I am trying to convert the following rewrite rule into a 'try_files':

location ~* \.(css|js) {
    if (!-f $request_filename) {
        rewrite ^/wp-content/w3tc/min/(.+\.(css|js))$ /wp-content/w3tc/min/index.php?file=$1 last;
        expires max;
    }
}

My new rule that I am shooting for:

location ~* \.(css|js) {
    try_files $uri /wp-content/w3tc/min/index.php?file=$request_basename;
    expires max;
}

A sample URI that is being generated:

http://mysite.com/wp-content/w3tc/min/88f17/default.include.60eaac.css

I found the following to get the basename of the URI:

map $request_uri $request_basename {
    ~/(?<captured_request_basename>[^/?]*)(?:\?|$) $captured_request_basename;
}

The problem I have is that '$request_basename' equals 'default.include.60eaac.css'. The application I am trying to work around (W3 Total Cache) needs '88f17/default.include.60eaac.css' passed to it.

Anyone know how I can modify the map rule to include the parent directory in the variable it generates? I kinda suck at regex...

share|improve this question
add comment

1 Answer

You can capture the basename in a location block:

location ~ ^/wp-content/w3tc/min/(?P<request_basename>.*\.(css|js))$ {
    try_files $uri /wp-content/w3tc/min/index.php?file=$request_basename;
}
share|improve this answer
add comment

Your Answer

 
discard

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.