Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 396765
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T16:35:04+00:00 2026-05-12T16:35:04+00:00

So Rails time stamping is great. I’m using it to add expires headers to

  • 0

So Rails time stamping is great. I’m using it to add expires headers to all files that end in the 10 digit timestamp. Most of my images however are referenced in my CSS. Has anyone come across any method that allows for timestamps to be added to CSS referenced images, or some funky re-write rule that achieves this? I’d love for ALL images in my site, both inline and in css to have this timestamp so I can tell the browser to cache them, but refresh any time the file itself changes.

I couldn’t find anything on the net regarding this and I can’t believe this isn’t a more frequently discussed topic.

I don’t think my setup will matter because the actual expiring will hopefully happen the same way, based on the 10 digit timestamp, but I’m using apache to serve all static content if that matters

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T16:35:04+00:00Added an answer on May 12, 2026 at 4:35 pm

    You can append the actual timestamp of each image file by getting the file modification time like this:

        source.gsub!(/url\((['"]*)(.+)(['"]*)\)/) do
          open, file, close = $1, $2, $3
          css_dir = File.join(RAILS_ROOT,"public/stylesheets")
          timestamp = ''
          FileUtils.cd(css_dir) do 
            if file =~ /^\// # absolute path
              f = File.new(RAILS_ROOT + "/public" + file)
            else # relative path
              f = File.new(file)
            end
            timestamp = f.mtime.to_i.to_s
          end
    
          if file =~ /.\.(ico|css|js|gif|jpe?g|png)/
            "url(#{open}#{file}?#{timestamp}#{close})"
          else
            "url(#{open}#{file}#{close})"
          end
        end
    

    (There’s probably a more elegant way to write this, my ruby chops are still weak!)
    Now the hack’s getting ugly, though… you’d think there would be a more Rails-like way to do this.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ruby on Rails has magic timestamping fields that are automatically updated when a record
I have a rails time-based query which has some odd timezone sensitive behaviour, even
Does anybody know any Ruby on Rails time_select plugins that have only one select
Since a short time my rails applications yields the following runtime error in the
I have been interested in learning Rails for some time now and feel now
I want to remove the : from the output of the rails time_select helper.
I would like to start another process when mongrel rails starts up. Is there
I am getting bad performance after upgrading an application to Rails 3. Before upgrading,
I have been practicing TDD in C# for several years now and recently have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.