SO I am trying to setup a RewriteRule on my server for caching static objects. the files are in this naming scheme /docroot/css/stylesheet.min.css and I have them printed in the code like /docroot/css/stylesheet.min.123438348.css (the number is example it comes from a get modified function). Note docroot is an example directory

how can I have the server ignore the numbers and redirect to the stylesheet.min.css I need to do this for every css and js files (/js and /css) as well as one specific spritemap image

my current attempt

RewriteRule ^/(docroot)/(js|css)/(.+)\.(min)\.(.+)\.(js|css)$ /$1/$2/$3.$4.$6
RewriteRule ^(/docroot/images/spritemap)\.([0-9]+)\.(png)$ $1.$3 

Update: Now I have the setup like this

<Location />
RewriteEngine on
Options FollowSymLinks
RewriteRule ^(.+)\.(min)\.([0-9]+)\.(js|css)$ $1.$2.$4 [L]

This is rewriting localhost/docroot/css/stylesheet.min.12343242.css to /var/www/html/docroot/trunk/docroot/css/stylesheet.min.css

so it is getting the right file how do I get apache to take off the beginning of the that the /var/www/html/docroot/trunk/

<Location />
    RewriteEngine on
    RewriteBase /
    RewriteRule ^(.+)\.(min)\.([0-9]+)\.(js|css)$ $1.$2.$4 [PT]

Options FollowSymLinks in Directory listing

Ok Now instead of


I am getting a url that looks like this


I Removed the RewriteBase command so I still need to remove the beginning /docroot/trunk


I suggest to try the following:

RewriteRule ^/(.+min)\.([0-9]+)\.(js|css)$ /$1.$3 [PT]

Apache sometimes has unpredictable behaviour when no flag is set. I recommend setting it explicitly every time. Also, period characters in regexes must be escaped - otherwise they mesn "any character".

I also recommend reading the official Apache docs on mod_rewrite over here: http://httpd.apache.org/docs/current/mod/mod_rewrite.html

  • This doesnt seem to match the url to the file. In my post above that expression matched the file and stripped out the number it just had the full path from filesystem. How do I strip that off too, this did not change anything – Bill Jan 6 '11 at 14:51
  • RewriteRule ^.+/(js|css)(.+)\.(min)\.([0-9]+)\.(js|css)$ /docroot/$1$2.$3.$5 This Works – Bill Jan 6 '11 at 17:51

Don't ever use Rewrite directives inside of <Location> -- just use them in virtualhost context if you're not using htaccess. This greatly simplifies every single aspect of using mod_rewrite, and removes the need for stripping of per-directory prefixes, RewriteBase, etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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