SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Evangelist
    Join Date
    May 2007
    Location
    Kent in Uk
    Posts
    538
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    setting expiry times for images to speed up site

    Hi,

    I recently went into Google speedinsights to get further information re whether my site is responding at optimum speed. I was told that i need to set expiry times on the images on my pages.

    How can I do this and what kind of expiry time should I set?

    thanks,

  2. #2
    Life is not a malfunction gold trophysilver trophybronze trophy
    TechnoBear's Avatar
    Join Date
    Jun 2011
    Location
    Argyll, Scotland
    Posts
    6,146
    Mentioned
    261 Post(s)
    Tagged
    5 Thread(s)
    If you're using Apache, you can add expiry information in your .htaccess file. For example:

    Code:
    # Enable the mod_expires module
    ExpiresActive On
    # Set expiration date to 1 month for all style sheets and images
    ExpiresByType text/css "access plus 1 month"
    ExpiresByType image/jpeg "access plus 1 month"
    ExpiresByType image/gif "access plus 1 month"
    ExpiresByType image/png "access plus 1 month"
    # Set expiration date to 1 week for all HTML pages
    ExpiresByType text/html "access plus 1 week"
    ExpiresByType application/xhtml+xml "access plus 1 week"
    What time-scale you choose depends on how likely you are to amend the image. The expiry is basically saying it's OK to serve a cached copy of this resource for another day/week/month or whatever after it's been accessed. For images, it's probably safe to set a lengthy expiry, because if you change the image, you're likely to replace it with one with a different file name i.e. a completely different resource.

    There's a good tutorial here that you might find useful.

  3. #3
    SitePoint Evangelist
    Join Date
    May 2007
    Location
    Kent in Uk
    Posts
    538
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by TechnoBear View Post
    If you're using Apache, you can add expiry information in your .htaccess file. For example:

    Code:
    # Enable the mod_expires module
    ExpiresActive On
    # Set expiration date to 1 month for all style sheets and images
    ExpiresByType text/css "access plus 1 month"
    ExpiresByType image/jpeg "access plus 1 month"
    ExpiresByType image/gif "access plus 1 month"
    ExpiresByType image/png "access plus 1 month"
    # Set expiration date to 1 week for all HTML pages
    ExpiresByType text/html "access plus 1 week"
    ExpiresByType application/xhtml+xml "access plus 1 week"
    What time-scale you choose depends on how likely you are to amend the image. The expiry is basically saying it's OK to serve a cached copy of this resource for another day/week/month or whatever after it's been accessed. For images, it's probably safe to set a lengthy expiry, because if you change the image, you're likely to replace it with one with a different file name i.e. a completely different resource.

    There's a good tutorial here that you might find useful.
    Thanks for this - but being a very green php person - would I put this into .htaccess exactly as it is above or would I need to add the names of the images?

  4. #4
    Life is not a malfunction gold trophysilver trophybronze trophy
    TechnoBear's Avatar
    Join Date
    Jun 2011
    Location
    Argyll, Scotland
    Posts
    6,146
    Mentioned
    261 Post(s)
    Tagged
    5 Thread(s)
    You don't need to specify images, just image type. So if, for example, you don't have any gifs, you can omit that line. Just include the lines that apply to your site. (Or use them all, if you think you might add other content-type in future and forget to update the .htaccess file. )


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •