Setup Http Expires Headers Using PHP and Apache

Setup HTTP expires headers using PHP and Apache

There are two ways to do this. The first is to specify the header in your php code. This is great if you want to programatically adjust the expiry time. For example a wiki could set a longer expires time for a page which is not edited very often.

header('Expires: '.gmdate('D, d M Y H:i:s \G\M\T', time() + (60 * 60))); // 1 hour

Your second choice is to create an .htaccess file or modify your httpd config. In a shared hosting environment, modifying your .htaccess file is quite common. In order to do this, you need to know if your server supports mod_expires, mod_headers or both. The easiest way is simply trial and error, but some Apache servers are configured to let you view this information via the /server-info page. If your server has both mod_expires and mod_headers, and you want to set the expiry on static resources, try putting this in your .htaccess file:

# Turn on Expires and set default to 0
ExpiresActive On
ExpiresDefault A0

# Set up caching on media files for 1 year (forever?)
<FilesMatch "\.(flv|ico|pdf|avi|mov|ppt|doc|mp3|wmv|wav)$">
ExpiresDefault A29030400
Header append Cache-Control "public"
</FilesMatch>

For other combinations and more examples see: http://www.askapache.com/htaccess/speed-up-your-site-with-caching-and-cache-control.html

Set expires headers without enabling mod_expires or mod_headers possible in php?

You cannot set something with PHP where PHP is not involved in. Generally your webserver serve images and static files, so the webserver have to handle the expire headers for you. All other things, for example serve images with PHP is really not recommended and requires a lot more work than just configuring the webserver properly.

So, expire headers for static files (images, javascript, etc...) is managed by your webserver, not PHP.

Tip: On most default webhosting services is apache installed, so you can use a .htaccess file to set those things properly.

Edit, more explanation:
You must difference between for what files you want to set expire headers. For sure, you can set expire header in your PHP files too but this only affect the pages that are served from PHP. And php is mostly used to display dynamic web pages, so an expire header here makes no sense. Static images and all those files never get passed to PHP so you have to set expire in the webserver config. And because images and other static files are static files that not change (or not often) it is recommend to set expire header to allow the browser to cache it properly

Apache: How to add Expires headers to files without mod_expires installed

In your .htaccess (if that's an option) you can use a <FilesMatch> block with Header directives. This requires mod_headers, though, and I'm pretty sure that you can't specify a "rolling" expiration date (i.e., "one year from now"). Therefore, you'll need to edit this setting, say, once a year1.

Also, did you see this question?


1) Apparently you should refrain from setting the Expires to more than a year into the future: "Do not set it [the Expires header] to more than one year in the future, as that violates the RFC guidelines." (source: Optimize caching)

Add Expire Headers in php can't make it work

That will only set it for your page content and not things like images and css files, i noticed on your screenshot it says 42 files, presumably these are your images, css, js etc.

Try this in your .htaccess file, note this will only work if you have mod_expires and mod_headers enabled in Apache:

<ifModule mod_expires.c>
ExpiresActive On
ExpiresDefault "access plus 1 seconds"
ExpiresByType text/html "access plus 1 seconds"
ExpiresByType image/gif "access plus 2592000 seconds"
ExpiresByType image/jpeg "access plus 2592000 seconds"
ExpiresByType image/png "access plus 2592000 seconds"
ExpiresByType text/css "access plus 604800 seconds"
ExpiresByType text/javascript "access plus 216000 seconds"
ExpiresByType application/x-javascript "access plus 216000 seconds"
ExpiresByType application/javascript "access plus 216000 seconds"
</ifModule>

<ifModule mod_headers.c>
<filesMatch "\\.(ico|pdf|flv|jpg|jpeg|png|gif|swf)$">
Header set Cache-Control "max-age=2592000, public"
</filesMatch>
<filesMatch "\\.(css)$">
Header set Cache-Control "max-age=604800, public"
</filesMatch>
<filesMatch "\\.(js)$">
Header set Cache-Control "max-age=216000, private"
</filesMatch>
<filesMatch "\\.(xml|txt)$">
Header set Cache-Control "max-age=216000, public, must-revalidate"
</filesMatch>
<filesMatch "\\.(html|htm|php)$">
Header set Cache-Control "max-age=1, private, must-revalidate"
</filesMatch>
</ifModule>

Add expires header without mod_expires?

You could use mod_header to set the header field manually:

Header set Expires "..."

But since Expires requires an absolute time, use Cache-Control’s max-age parameter for times relative to the access time:

Header merge Cache-Control max-age=3600


Related Topics



Leave a reply



Submit