Adding Expires Headers and .htaccess
Asked Answered
C

2

7

I'm trying to optimise a WordPress site of mine - Type & Music based on reports I've been getting from GTmetrix. One of the things I'm being advised to do is add expires headers and leverage browser caching. I may be wrong, but I assume these are kind of the same thing?

Anyways, I've been looking at tutorials online such as How to Add Far Future Expires Headers to Your WordPress Site and How to Leverage Browser Caching in WordPress via .htaccess from Thomas Griffen Media, which all seem to be just a case of copy and paste (I am aware that the settings are specific to each site though, depending how how regularly you update/edit certain content and files) but these settings never seem to register at all when I retest the site.

Here are the contents of my .htaccess file:

# BEGIN WordPress

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

## EXPIRES CACHING ##
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access 1 month”
ExpiresByType image/jpeg "access 1 month”
ExpiresByType image/gif "access 1 year"
ExpiresByType image/png "access 1 week”
ExpiresByType text/css "access 1 week”
ExpiresByType application/pdf "access 1 month"
ExpiresByType text/x-javascript "access 1 week"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresByType image/x-icon "access 1 year"
ExpiresDefault "access 2 days"
</IfModule>
## EXPIRES CACHING ##

I have since installed the Quick Cache plugin too, but to the best of my knowledge that doesn't really conflict, especially since the settings in my htaccess file aren't registering in the first place.

Any help would be greatly appreciated as I've found nothing of help myself.

Consignment answered 6/2, 2014 at 13:24 Comment(1)
Did either of the answers help you?Statolith
G
7

Here's what you need to add to your .htaccess file in order to get rid of this problem. This is the entire script for most type of files. Hope this helps.

<IfModule mod_expires.c>
    ExpiresActive on
    ExpiresDefault "access plus 1 month"
    ExpiresByType image/gif "access plus 1 month"
    ExpiresByType image/png "access plus 1 month"
    ExpiresByType image/jpg "access plus 1 month"
    ExpiresByType image/jpeg "access plus 1 month"
    ExpiresByType text/html "access plus 3 days"
    ExpiresByType text/xml "access plus 1 seconds"
    ExpiresByType text/plain "access plus 1 seconds"
    ExpiresByType application/xml "access plus 1 seconds"
    ExpiresByType application/rss+xml "access plus 1 seconds"
    ExpiresByType application/json "access plus 1 seconds"
    ExpiresByType text/css "access plus 1 week"
    ExpiresByType text/javascript "access plus 1 week"
    ExpiresByType application/javascript "access plus 1 week"
    ExpiresByType application/x-javascript "access plus 1 week"
    ExpiresByType image/x-ico "access plus 1 year"
    ExpiresByType image/x-icon "access plus 1 year"
    ExpiresByType application/pdf "access plus 1 month"
  <IfModule mod_headers.c>
       Header unset ETag
       Header unset Pragma
       Header unset Last-Modified
       Header append Cache-Control "public, no-transform, must-revalidate"
       Header set Last-modified "Tue, 1 Oct 2014 10:10:10 GMT"
  </IfModule>
</IfModule>
Goldstone answered 28/7, 2015 at 19:23 Comment(0)
C
1

The problem has to do with missing Apache2 Modules on your LAMP server (specifically mod_headers and mod_expires). If you can get in via SSH, go there and access root through the following command:

sudo -i

Then paste in the following to enable Mod Headers:

sudo a2enmod headers

You will then see this message: To activate the new configuration, you need to run: service apache2 restart

Paste in the command below to restart:

service apache2 restart

Next, paste the command below to enable content caching:

sudo a2enmod expires

Restart again with what you see below and you will be good:

service apache2 restart

That's it. Everything hosted on your server should now be caching correctly. Note that if you're using a CDN, you will need to go there and specify a far future cache date as well. If you're using MaxCDN, you just navigate to Zones > Pull Zones > Settings > Cache Settings, then dictate 12 months.

Croner answered 7/3, 2018 at 5:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.