可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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 Google Analytics - it has expire headers set to 1 day.
Google is not my problem, some other scripts from external websites are the real problem, they don't have expire headers at all.
回答1:
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.
回答2:
The only way is to create script which downloads contents from external site and then adds needed headers.
<script type="text/javascript" src="http://external.example.com/foo.js"></script>
To
<script type="text/javascript" src="external.php?url=http://external.example.com/foo.js"></script>
And external.php is something like
<?php
header("Expire-stuff: something");
echo file_get_contents($_GET['url']);
Of course this has security hole so I'd recommend to use identifier strings like external.php?file=foo.js and then using
$files = array('foo.js' => 'http://external/...');
if(isset($files[$_GET['file']]))
{
echo file_get_contents($files[$_GET['file']]);
}
file_get_contents() of course will take some of your bandwith so it would be recommended to cache the result also.
回答3:
Thats not possible.
Not recommended (and not always possible): If its static content, prefetch it with a script and set your own headers.
回答4:
You could dynamically load the external pages using PHP, so you can send headers before outputting the original data. This is not an ideal solution but if you really have to you may want to use it.
<?php
header('expire-header');
echo file_get_contents('http://www.extern.al/website/url');
回答5:
You can't.
Try e-mailing the one(s) hosting the file and try to get them to apply expires-headers to it.
回答6:
Don't lose your mind for these page tests... some of the recommendations may be useful and some of them you can't do anything. Do whatever you can do with your own files, don't mind about external ones.
回答7:
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
回答8:
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"
回答9:
You may be able to add a query string parameter to fool the browser into thinking it's requesting a different resource. For example, if you want the browser to never cache a CSS, you can add a question mark followed by a random number to the end of the URL. This usually works but can be made to not work by the server hosting the file. Try it and see.