Search code examples
apachemod-rewritemod-expires

Apache: apply rules to a URL before they're rewritten


I have a simple RewriteRule:

RewriteRule ^/r/[0-9]+/(.*)$ /$1

This is used for cache-busting. With every web site release I change the url prefix, e.g.:

/r/17/img/image.jpg gets /img/image.jpg.

I want to apply long expiry headers to these for example

<Directory /r>
  Header unset ETag
  FileETag None
  ExpiresDefault "access plus 1 year"
</Directory>

Of course this doesn't work because after the RewriteRule is applied, the Directory doesn't match anymore. How can I apply these rules inside the Directory directive to URLs accessed via /r/ ?

Thanks!


Solution

  • The <Directory> directive is for actual existing directories and not just URL paths. Try <LocationMatch> instead:

    <LocationMatch "^/r(/|$)">
      Header unset ETag
      FileETag None
      ExpiresDefault "access plus 1 year"
    </LocationMatch>
    

    Or change /r to your actual directory /img.