How to put an expires header on a PHP file which outout a JS file?
.htaccess
ExpiresActive on
ExpiresByType image/gif A29030400
ExpiresByType image/jpeg A29030400
ExpiresByType image/png A29030400
ExpiresByType text/css A29030400
ExpiresByType application/javascript A29030400
header on JS file
Cache-Control max-age=29030400
Connection Keep-Alive
Date Thu, 18 Oct 2012 09:23:16 GMT
Etag "300000002c8ba-15f-4cc3069c72d00"
Expires Thu, 19 Sep 2013 09:23:16 GMT
Keep-Alive timeout=5, max=94
Server Apache/2.2.22 (Win32) PHP/5.4.3
header on PHP file which outputs a JS file
Cache-Control no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Connection Keep-Alive
Content-Length 1195
Content-Type application/javascript
Date Thu, 18 Oct 2012 09:23:16 GMT
Expires Thu, 19 Nov 1981 08:52:00 GMT
Keep-Alive timeout=5, max=100
Pragma no-cache
Server Apache/2.2.22 (Win32) PHP/5.4.3
X-Powered-By PHP/5.4.3
code PHP file
<?php
header('Content-Type: application/javascript');
$js_output = array('something' => 'some value');
?>
var <?=json_encode($js_output)?>
You need to keep pretty much same Expire setting in your .htaccess i.e.:
Then in your PHP code which is writing JS file use correct
Content-Type
and :at the top. Monitor your headers in Firebug and it should show you something like this: