2

Following htaccess code is available at siteground for Leverage browser caching. My question is that I want to set images expiration time for 1 week and css and scripts expiration time for 2 days. How can I do this? as it is already mentioned in code "now plus 1 month or 1 week after request", but I am not getting it as I am newbie. Can someone please suggest or change below htaccess as per desired? Looking forward for your kind co-operation.

########## Begin - ETag Optimization
## This rule will create an ETag for files based only on the modification
## timestamp and their size. 
## Note: It may cause problems on your server and you may need to remove it
FileETag MTime Size
# AddOutputFilterByType is now deprecated by Apache. Use mod_filter in the future.
AddOutputFilterByType DEFLATE text/plain text/html text/xml text/css application/xml application/xhtml+xml application/rss+xml application/javascript application/x-javascript
# Enable expiration control
ExpiresActive On
# Default expiration: 1 hour after request
ExpiresDefault "now plus 1 hour"
# CSS and JS expiration: 1 week after request
ExpiresByType text/css "now plus 1 week"
ExpiresByType application/javascript "now plus 1 week"
ExpiresByType application/x-javascript "now plus 1 week"

# Image files expiration: 1 month after request
ExpiresByType image/bmp "now plus 1 month"
ExpiresByType image/gif "now plus 1 month"
ExpiresByType image/jpeg "now plus 1 month"
ExpiresByType image/jp2 "now plus 1 month"
ExpiresByType image/pipeg "now plus 1 month"
ExpiresByType image/png "now plus 1 month"
ExpiresByType image/svg+xml "now plus 1 month"
ExpiresByType image/tiff "now plus 1 month"
ExpiresByType image/vnd.microsoft.icon "now plus 1 month"
ExpiresByType image/x-icon "now plus 1 month"
ExpiresByType image/ico "now plus 1 month"
ExpiresByType image/icon "now plus 1 month"
ExpiresByType text/ico "now plus 1 month"
ExpiresByType application/ico "now plus 1 month"
ExpiresByType image/vnd.wap.wbmp "now plus 1 month"
ExpiresByType application/vnd.wap.wbxml "now plus 1 month"

1 Answer 1

3

First take the backup of your current htaccess file and then try this :-

List item

#### Begin - ETag Optimization

This rule will create an ETag for files based only on the modification

timestamp and their size.

Note: It may cause problems on your server and you may need to remove it

FileETag MTime Size

AddOutputFilterByType is now deprecated by Apache. Use mod_filter in the future.

AddOutputFilterByType DEFLATE text/plain text/html text/xml text/css application/xml application/xhtml+xml application/rss+xml application/javascript application/x-javascript

Enable expiration control

ExpiresActive On

Default expiration: 1 hour after request

ExpiresDefault "now plus 1 hour"

CSS and JS expiration: 2 days after access

ExpiresByType text/css "access plus 2 days"
ExpiresByType application/javascript "access plus 2 days"
ExpiresByType application/x-javascript "access plus 2 days"

Image files expiration: 1 week after access

ExpiresByType image/bmp "access plus 1 week"
ExpiresByType image/gif "access plus 1 week"
ExpiresByType image/jpeg "access plus 1 week"
ExpiresByType image/jp2 "access plus 1 week"
ExpiresByType image/pipeg "access plus 1 week"
ExpiresByType image/png "access plus 1 week"
ExpiresByType image/svg+xml "access plus 1 week"
ExpiresByType image/tiff "access plus 1 week"
ExpiresByType image/vnd.microsoft.icon "access plus 1 week"
ExpiresByType image/x-icon "access plus 1 week"
ExpiresByType image/ico "access plus 1 week"
ExpiresByType image/icon "access plus 1 week"
ExpiresByType text/ico "access plus 1 week"
ExpiresByType application/ico "access plus 1 week"
ExpiresByType image/vnd.wap.wbmp "access plus 1 week"
ExpiresByType application/vnd.wap.wbxml "access plus 1 week"

4
  • Thanks for your input. Default expiration: 1 hour after request ExpiresDefault "now plus 1 hour" What does it mean? As there is already set expiration on images/css/scripts as per desired. But I did not get it ExpiresDefault "now plus 1 hour"
    – Naeem
    Commented Sep 15, 2014 at 17:00
  • apart from css and images there are other file types too like js,html etc, so default expires headers sets the time for all other file types that we are not defining explicitly.It will increase site performance.
    – Ignis
    Commented Sep 18, 2014 at 12:20
  • I have added above code to my htaccess then i checked score on gtmetrix, which increased. But total number of request has been increased from 160 to 200. Why 40 more request has incrased?
    – Naeem
    Commented Sep 18, 2014 at 19:00
  • is it good to set one week for ExpiresDefault "now plus 1 hour" ?
    – Naeem
    Commented Dec 26, 2014 at 10:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.