I have a website and I added the expire headers on all pages/images and scripts but I don’t know how I could add expire headers to external scripts.
For example Goo
You can only add header fields in responses to requests that go to your own server. If the request goes to another server, say Google’s server, than it’s Google’s server that answers the request.
So the only solution to your problem is hosting that external resources on your own server. But that’s only possible if that resources are static, do not change from request to request and do not depend on other things.
I have made a version of that code that let you specify different expire dates for each scripts:
<?php
$files = array(
'ga.js' => 'https://ssl.google-analytics.com/ga.js',
'bsa.js' => 'https://s3.buysellads.com/ac/bsa.js',
'pro.js' => 'https://s3.buysellads.com/ac/pro.js'
);
if(isset($files[$_GET['file']])) {
if ($files[$_GET['file']] == 'ga.js'){
header('Expires: '.gmdate('D, d M Y H:i:s \G\M\T', time() + ((60 * 60) * 48))); // 2 days for GA
} else {
header('Expires: '.gmdate('D, d M Y H:i:s \G\M\T', time() + (60 * 60))); // Default set to 1 hour
}
echo file_get_contents($files[$_GET['file']]);
}
?>
More info: https://www.catswhocode.com/blog/php-how-to-add-expire-headers-for-external-scripts
The following may be useful for you.
ExpiresActive On
ExpiresDefault "access plus 1 seconds"
ExpiresByType image/x-icon "access plus 2692000 seconds"
ExpiresByType image/jpeg "access plus 2692000 seconds"
ExpiresByType image/png "access plus 2692000 seconds"
ExpiresByType image/gif "access plus 2692000 seconds"
ExpiresByType application/x-shockwave-flash "access plus 2692000 seconds"
ExpiresByType text/css "access plus 2692000 seconds"
ExpiresByType text/javascript "access plus 2692000 seconds"
ExpiresByType application/x-javascript "access plus 2692000 seconds"
ExpiresByType text/html "access plus 600 seconds"
ExpiresByType application/xhtml+xml "access plus 600 seconds"