Benno
Benno

Reputation: 3008

RewriteRule for JS cache busting

I use cache busting query strings on my JavaScript/CSS, and just wondering how I can rewrite that so its part of the filename instead?

I want to rewrite: website.com/js/m/file.min.js?params to website.com/js/m/file.min.params.js instead.

  <IfModule mod_rewrite.c>

      # Domain Redirects

      RewriteEngine On
      RewriteBase /

      RewriteRule ^/js/m/([a-zA-Z.]+).min.[0-9]+.js$ /js/m/$1.min.js [L]

      RewriteCond %{REQUEST_FILENAME} -s [OR]
      RewriteCond %{REQUEST_FILENAME} -l [OR]
      RewriteCond %{REQUEST_FILENAME} -d
      RewriteRule ^.*$ - [NC,L]

      #If we're here, redirect through zend:
      RewriteRule ^.*$ index.php [NC,L]
    </IfModule>

Upvotes: 2

Views: 1064

Answers (1)

anubhava
anubhava

Reputation: 784958

First of all your this rule is incorrect and won't work:

RewriteRule ^/js/m/([a-zA-Z.]+).min.[0-9]+.js$ /js/m/$1.min.js [L]

Reason: RewriteRule matches URI without a leading slash.

Now your task:

I want to rewrite: website.com/js/m/file.min.js?params to website.com/js/m/file.min.params.js instead.

Can be handled by this rule:

# redirect from /js/m/file.min.js?params to /js/m/file.min.params.js
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(js/m/.+?)\.js\?([^\s&]+) [NC]
RewriteRule ^ /%1.%2.js? [R=301,L]

# forward from /js/m/file.min.params.js to /js/m/file.min.js?params
RewriteRule ^(js/m/.+?\.min)\.([^.]+)\.js$ $1.js?$2 [L,QSA,NC]

Upvotes: 2

Related Questions