I have this file in my webserver:
http://example.com/static/js/min/common.min.js
Since all files inside /static/
are cached with CloudFlare's Edge Cache, I need a way to change the url with something like this, so if the file is modified, the new version will be automatically fresh served:
http://example.com/static/js/min/common.min.1234567890.js
Where 1234567890 is the timestamp of the file's date modification. I already generate the filename according to the modification date, the issue I'm having is in the .htaccess file.
This works fine:
RewriteRule ^(.*)\.[\d]{10}\.(js)$ $1.$2 [L]
That means that:
http://example.com/static/js/min/common.min.1234567890.js
Is redirected to:
http://example.com/static/js/min/common.min.js
But, that will catch all .js requests from the domain, I just want to catch .js requests from within /static/js/min/*.js -- but this is failing:
RewriteRule ^static/js/min/(.*)\.[\d]{10}\.(js)$ $1.$2 [L]
What should the rule be like?
1 Answers
Answers 1
From your question,
You want to redirect
http://example.com/static/js/min/common.min.1234567890.js
to
http://example.com/static/js/min/common.min.js
So, How to do that
the .htaccess
First add
RewriteEngine On
To turn on the rewrite engine
Then the next important line comes
RewriteRule ^static/js/min/([^\d]*)(\d{10}).js$ static/js/min/$1js [L]
Explanation
Set the path as static/js/min/
Then we use RegEx to take the string until a non digit. ([^\d]*)
.
That is common.min.
is captured.
Now $1
is common.min.
Then to match the url, we use (\d{10})
to capture the digits.
That is 1234567890
is captured.
Now $2
is 1234567890
which we don't want anymore
Then redirect to
static/js/min/$1js
Not that here we didn't added the .
after the $1
because $1
ending with a .
(common.min.
)
So, the code will be
RewriteEngine On RewriteRule ^static/js/min/([^\d]*)(\d{10}).js$ static/js/min/$1js [L]
Working Screenshot
My File Structure
The Address in Browser
0 comments:
Post a Comment