ConfusedAmish
ConfusedAmish

Reputation: 291

Apache Rules for JS/CSS versioning using mod_rewrite

I've been trying this for the last 2h.

how can I use mod rewrite to do this:

/assets/app.min.4364736473.js -> /assets/app.min.js
/assets/app.min.4364736473.css -> /assets/app.min.css

Upvotes: 2

Views: 1227

Answers (2)

user2493235
user2493235

Reputation:

Not that there's anything wrong with nikoshr's answer, but perhaps this is more versatile, working anywhere in the site that the format is used, since it's not likely to be used for any usual extension. In case it is, it checks that the file with the number doesn't exist before executing. The .min is optional, it works with or without it:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)\.\d+(\.(?:js|css))$ $1$2

Finally, not using the [L] flag since other rules might need to be applied on the rewritten URL and no need to stop that happening.

It will only work properly in a .htaccess or <Directory> context. Otherwise (in a root config or <VirtualHost> context) it will need modifying for the file test to work or existing files won't be noticed and it will execute anyway.

Upvotes: 5

nikoshr
nikoshr

Reputation: 33344

It could be

RewriteEngine on
RewriteRule /assets/app\.min\.\d+\.js$ /assets/app.min.js [L]
RewriteRule /assets/app\.min\.\d+\.css$ /assets/app.min.css [L]

Upvotes: 2

Related Questions