# Question about rewrite directive

**URL:** <https://www.ruby-forum.com/t/question-about-rewrite-directive/243960>\
**Category:** NGINX\
**Created:** [January 28, 2016, 3:12pm UTC](https://www.ruby-forum.com/t/question-about-rewrite-directive/243960 "2016-01-28T15:12:33Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sadaf\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@Sadaf\_N](https://www.ruby-forum.com/u/Sadaf_N)\
**Post date:** [January 28, 2016, 3:12pm UTC](https://www.ruby-forum.com/t/question-about-rewrite-directive/243960/1 "2016-01-28T15:12:33Z")

</div>

Hi,

a customer has this in his .htaccess file (among other things):

RewriteCond %{REQUEST\_FILENAME} !-f  
RewriteCond %{REQUEST\_FILENAME} !-d  
RewriteRule ^(.+).(\d+).(php|js|css|png|jpg|gif|gzip)$ $1.$3 [L]

This is to enable versioning of various files, so you can have long  
“Expires” on them and still update them as needed while retaining the  
old ones, if needed.

I want to deliver static files directly from nginx, so I created this:

```
 location ~* ^(.+)\.(\d+)\.(js|css|png|jpg|gif|gzip)$ {
            rewrite ^(.+)\.(\d+)\.(js|css|png|jpg|gif|gzip)$ $1.$3 ;
            expires 1h;
 }

```

This works in most cases, except for files which already have a version  
number of some sort.  
Namely:  
coda-slider.1.1.1.1452703531.js  
and two others from the jquery framework.

What’s wrong with my nginx rewrite?  
Because in apache, the rewrite rule works as intended.

nginx 1.8.0 on FreeBSD 10-amd64.

Regards  
Rainer

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [January 28, 2016, 10:04pm UTC](https://www.ruby-forum.com/t/question-about-rewrite-directive/243960/2 "2016-01-28T22:04:59Z")

</div>

> This is to enable versioning of various files, so you can have long  
> “Expires” on them and still update them as needed while retaining  
> the old ones, if needed.
> 
> I want to deliver static files directly from nginx, so I created this:

Not exactly sure about the notation in nginx but for regexp what  
about:

> ```
> location ~* ^(.+)\.(\d+)\.(js|css|png|jpg|gif|gzip)$ {
> 
> ```

```
  location ~* ^(.+)\.([\d\.]+)\.(js|css|png|jpg|gif|gzip)$ {

```

## wbr Lukas

Lukas Ruf [http://www.lpr.ch](http://www.lpr.ch) | Ad Personam  
Consecom [http://www.consecom.com](http://www.consecom.com) | Ad Laborem

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [January 28, 2016, 11:48pm UTC](https://www.ruby-forum.com/t/question-about-rewrite-directive/243960/3 "2016-01-28T23:48:58Z")

</div>

On Thu, Jan 28, 2016 at 03:12:16PM +0100, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

Hi there,

> RewriteCond %{REQUEST\_FILENAME} !-f  
> RewriteCond %{REQUEST\_FILENAME} !-d  
> RewriteRule ^(.+).(\d+).(php|js|css|png|jpg|gif|gzip)$ $1.$3 [L]

> ```
> location ~* ^(.+)\.(\d+)\.(js|css|png|jpg|gif|gzip)$ {
> rewrite ^(.+)\.(\d+)\.(js|css|png|jpg|gif|gzip)$ $1.$3 ;
> expires 1h;
> }
> 
> ```

> What’s wrong with my nginx rewrite?  
> Because in apache, the rewrite rule works as intended.

I see two main differences there:

Your apache RewriteRule has [L] on the end. Your nginx rewrite does  
not. Possibly you want “break” – [Module ngx\_http\_rewrite\_module](http://nginx.org/r/rewrite)

Your apache RewriteRule is protected by RewriteCond. Your nginx rewrite  
is not. Possibly something involving try\_files or error\_page and a named  
location for fallback could achieve the same effect.

## f

Francis D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [January 29, 2016, 12:15am UTC](https://www.ruby-forum.com/t/question-about-rewrite-directive/243960/4 "2016-01-29T00:15:16Z")

</div>

> > > RewriteRule ^(.+).(\d+).(php|js|css|png|jpg|gif|gzip)$ $1.$3 [L]  
> > > location ~\* ^(.+).(\d+).(js|css|png|jpg|gif|gzip)$ {  
> > > coda-slider.1.1.1.js
> 
> since the first series of digit-dot is matched by a).

Which is correct.

> If your customer has a file to be delivered that is named for example
> 
> linux-4.2.1.gzip
> 
> your regular expression would return
> 
> linux-4.2.gzip
> 
> since it strips off just the last digits-dot pair.

OK, that would be sub-optimal 😉

On request, the customer switched off the cache-breaking, so that  
problem has been solved.

As for the regex itself, I checked it in [regex101.com](http://regex101.com) and it did match  
the files.

The customer has elected not to use typo3’s static file-cache and serve  
every page from typo3’s page-cache inside the database.

To get some sort of sanity, I want to micro-cache all successful GET  
requests for a minute.

Thanks for your ideas.

Rainer

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [January 29, 2016, 12:07am UTC](https://www.ruby-forum.com/t/question-about-rewrite-directive/243960/5 "2016-01-29T00:07:05Z")

</div>

> > This is to enable versioning of various files, so you can have long  
> > “Expires” on them and still update them as needed while retaining  
> > the old ones, if needed.
> > 
> > I want to deliver static files directly from nginx, so I created this:
> 
> Not exactly sure about the notation in nginx but for regexp what  
> about:
> 
> > ```
> > location ~* ^(.+)\.(\d+)\.(js|css|png|jpg|gif|gzip)$ {
> > 
> > ```

Btw.

```
a) ".+" matches a series of any character
b) "\d+" matches a series of any digit

```

> coda-slider.1.1.1.1452703531.js

would then be returned as

```
coda-slider.1.1.1.js

```

since the first series of digit-dot is matched by a).

If your customer has a file to be delivered that is named for example

```
linux-4.2.1.gzip

```

your regular expression would return

```
linux-4.2.gzip

```

since it strips off just the last digits-dot pair.

wbr  
Lukas

–  
Lukas Ruf [http://www.lpr.ch](http://www.lpr.ch) | Ad Personam  
Consecom [http://www.consecom.com](http://www.consecom.com) | Ad Laborem
