1

I would like clients to be able to access certain files using an arbitrary version number to bypass caching.

For example, suppose there are two files: styles.css and jquery.min.js They should be able to request either the originals or styles.23.css and jquery.min.5039.css.

The rule I came up with was:

RewriteEngine On
RewriteRule ^(.*)\.(?!\..*)[\d]+\.(.*)$ $1.$2 # strip out version number
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]

Broken down, here's my thought on what it should be doing:

^(.*) – starting from the beginning, match all
\. – up to the first period...
(?!\..*) - ...which is not followed by a period and anything,
[\d]+\. – then match if ends in one or more digits followed by a period...
(.*)$ – ...and anything

This RegEx actually works seems to work in PHP but not .htaccess, which has me a bit confused.

Thank you in advance.

1 Answer 1

1

Why do you need lookahead etc. Following should work:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+?)\.\d+\.(.*)$ $1.$2 [L]
Sign up to request clarification or add additional context in comments.

1 Comment

Hi, thank you. I had completely forgotten about putting an additional "%{REQUEST_FILENAME} !-f" before the rule when testing various patterns, so they all failed at one point or another. I ended up using a variation of yours except instead of (.+?) at the beginning (which removes the first set of digits), I used (.*) which effectively removes the last, so file.12.34.123456.js loads file.12.34.js.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.