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

  • SEARCH
  • Home
  • 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 828785
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T03:46:45+00:00 2026-05-15T03:46:45+00:00

The google page speed tool recommends me to set ‘Expires’ headers for images etc.

  • 0

The google page speed tool recommends me to set ‘Expires’ headers for images etc. But what is the most efficient way to set an Expires header for an image?

In now redirect all image requests to an imagehandler.php using htaccess:

/*
HTTP/1.1 404 Not Found, HTTP/1.1 400 Bad Request and content type detection stuff
...
*/

header( "Content-Type: " . $content_type );
header( "Cache-Control: public" );
header( "Last-Modified: ".gmdate("D, d M Y H:i:s", filemtime($path))." GMT");
header( "Expires: ". date("r",time() + (60*60*24*30)));
readfile( $path );

But of course this adds extra loading time for my images on first request, and I was wondering if there was a better solution for this.

  • 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-15T03:46:45+00:00Added an answer on May 15, 2026 at 3:46 am

    You can add it in the .htaccess file.

    <FilesMatch "\.(ico|jpg|jpeg|png|gif)$">
    Header set Cache-Control "max-age=290304000, public"
    </FilesMatch>
    

    Found on AskApache.

    Of course, if you want the images changed, they won’t be downloded again until they expire.

    You can solve that by doing something like this

    function getImage($path) {
        // may need to add a DOCROOT constant here before filemtime() argument
        return $path . '?m=' . substr(filemtime($path) -5);
    
    }
    

    I just use the substr() to make it a little shorter. The chances of them colliding is minimum, but may happen. Be sure to test it.

    Use it like this

    <img src="<?php echo getImage('path/to/your/image.jpg'); ?>" alt="" />
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to set a cookie in my Google App Engine page: self.response.headers.add_header('Set-Cookie','CookieName=1234;
My website has about 20 thousand product images. Google Page Speed tells me they
I'm using google page speed and it's telling me my css is inefficient... Very
I have an ASP.NET website set up, and I'm using Google Analytics for page
Google Page Speed says. The following publicly cacheable, compressible resources should have a Vary:
I just installed Google's Page Speed plugin to Firebug, and everything looks good except
I just ran Google's Page Speed application against our site and one of the
I've been playin with Google's Page Speed, and it gave me some tips to
I have an issue which has arisen using Google Page Speed Online although I
I ran the Google Page Speed Firefox extension on a few pages, and under

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.