# Expires Headers with Rails

**URL:** <https://www.ruby-forum.com/t/expires-headers-with-rails/173782>\
**Category:** Rails\
**Created:** [August 14, 2009, 6:33pm UTC](https://www.ruby-forum.com/t/expires-headers-with-rails/173782 "2009-08-14T18:33:14Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![elricstorm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/da6949/32.png) [@elricstorm](https://www.ruby-forum.com/u/elricstorm)\
**Post date:** [August 14, 2009, 6:33pm UTC](https://www.ruby-forum.com/t/expires-headers-with-rails/173782/1 "2009-08-14T18:33:14Z")

</div>

Hi guys,

I have been trying numerous methods to get expires headers to work with  
apache and rails. I’m on a debian ubuntu server with:

sudo a2enmod rewrite expires headers deflate

… all on …

I’m using something similar in my Virtual site under the directory  
block:

# gzip html, css and js

AddOutputFilterByType DEFLATE text/html text/css  
application/x-javascript application/javascript

# far future expires headers

ExpiresActive On  
\<FilesMatch “.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)?d{10}$”\>  
ExpiresDefault “access plus 10 years”

I’m not using 10 years though, this is just an example script. I also  
tried:

# add something we can do a directory match on

RewriteCond %{QUERY\_STRING} ^[0-9]{10}$  
RewriteRule ^(.\*)$ /add\_expires\_header%{REQUEST\_URI} [QSA]

# the add\_expires\_header directory is just a symlink to public

\<Directory “/path/to/rails\_app/public/add\_expires\_header”\>  
ExpiresActive On  
ExpiresDefault “access plus 10 years”

and added the symlink for :

cd /path/to/rails\_app/public  
ln -s . add\_expires\_header

The problems I face are the following:

I want to use expire headers with all of the following content:

ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf

I also want the expire headers to work with images being loaded from CSS  
files. Rails puts the datetime stamp on all of my content so it should  
work with the ?datetimestamps too.

I’m sure someone here has setup something that works well. I could use  
some help to get this working. In my case, sometimes the rails  
?timestamps are ignored and YSlow shows the content not being expired…

Thanks.

---

<div class="post-metadata">

**Author:** ![elricstorm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/da6949/32.png) [@elricstorm](https://www.ruby-forum.com/u/elricstorm)\
**Post date:** [August 15, 2009, 6:33pm UTC](https://www.ruby-forum.com/t/expires-headers-with-rails/173782/2 "2009-08-15T18:33:34Z")

</div>

I decided to wait 24 hours before rebumping my thread. I still could  
use some assistance with this. I’m not sure how many out there use  
expire headers but I would like to accomplish this task with rails.

Please look over my former response and if you have expires headers  
working with your site, I could use some pointers for mine.

Many thanks.

---

<div class="post-metadata">

**Author:** ![elricstorm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/da6949/32.png) [@elricstorm](https://www.ruby-forum.com/u/elricstorm)\
**Post date:** [August 15, 2009, 7:23pm UTC](https://www.ruby-forum.com/t/expires-headers-with-rails/173782/3 "2009-08-15T19:23:25Z")

</div>

On Aug 14, 5:33 pm, Alpha B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> block:
> 
> # gzip html, css and js
> 
> AddOutputFilterByType DEFLATE text/html text/css  
> application/x-javascript application/javascript
> 
> # far future expires headers
> 
> ExpiresActive On  
> \<FilesMatch “.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)?d{10}$”\>

Don’t think that will actually match your files because…

- the d is missing a \
- the ? will be interpreter as a quantifier not a literal ?

Also you probably want the . to be literal dot, but that won’t prevent  
it matching

You could check all this be looking at what expires headers are  
actually being generated.

Fred

---

<div class="post-metadata">

**Author:** ![elricstorm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/da6949/32.png) [@elricstorm](https://www.ruby-forum.com/u/elricstorm)\
**Post date:** [August 15, 2009, 11:59pm UTC](https://www.ruby-forum.com/t/expires-headers-with-rails/173782/4 "2009-08-15T23:59:40Z")

</div>

On Aug 15, 8:54 pm, Alpha B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> my-image.gif?(timestamp) - those are not being expired.
> 
> In addition, any image content served from my css files:
> 
> background-image: url(/images/background\_defense.jpg);

Well those don’t have a timestamp on them so don’t match your  
filesmatch clause. Have you tried fiddling with the regular expression  
(or using ExpiresByType) ?

Fred

---

<div class="post-metadata">

**Author:** ![elricstorm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/da6949/32.png) [@elricstorm](https://www.ruby-forum.com/u/elricstorm)\
**Post date:** [August 15, 2009, 9:54pm UTC](https://www.ruby-forum.com/t/expires-headers-with-rails/173782/5 "2009-08-15T21:54:10Z")

</div>

Hi Fred,

So far, the only things being expired are the following:

All .css  
All .js  
All images served by rails that don’t contain \_ underscores or -  
hyphens.

If an image served by rails is for instance my\_image.gif?(timestamp) or  
my-image.gif?(timestamp) - those are not being expired.

In addition, any image content served from my css files:

background-image: url(/images/background\_defense.jpg);

is also not being expired.

==============

I changed the piece you mentioned to:

# far future expires headers

ExpiresActive On  
\<FilesMatch “.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)?d{10}$”\>  
ExpiresDefault “access plus 10 years”

… the same thing still occurs.

Any ideas? Many thanks mate.

---

<div class="post-metadata">

**Author:** ![elricstorm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/da6949/32.png) [@elricstorm](https://www.ruby-forum.com/u/elricstorm)\
**Post date:** [August 16, 2009, 3:13am UTC](https://www.ruby-forum.com/t/expires-headers-with-rails/173782/6 "2009-08-16T03:13:54Z")

</div>

Fred, good call mate.

ExpiresDefault worked great for css, js, and swf  
ExpiresBytype worked great for all images, including those within css

I pushed the expires to access plus 4 months since that’s the football  
season length and if any site modifications occur, they’ll happen after  
that time.

My site now grades out as an A in Expire Headers with YSlow.

Overall, my entire site grades out as a B with YSlow, almost at an A.  
It’s pretty speedy too.

Take care,

---

<div class="post-metadata">

**Author:** ![elricstorm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/da6949/32.png) [@elricstorm](https://www.ruby-forum.com/u/elricstorm)\
**Post date:** [August 17, 2009, 5:22pm UTC](https://www.ruby-forum.com/t/expires-headers-with-rails/173782/7 "2009-08-17T17:22:43Z")

</div>

Alpha B. wrote:  
[…]

> My site now grades out as an A in Expire Headers with YSlow.
> 
> Overall, my entire site grades out as a B with YSlow, almost at an A.  
> It’s pretty speedy too.

Don’t put too much faith in YSlow. Remember that it was designed to be  
an internal tool at Yahoo!. That means that it’s great if you’re Yahoo!  
or if you have similar usage patterns, but if you don’t, it can be very  
misleading. For example, on my projects, I’ve found most components of  
it nearly useless.

> Take care,

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
