views:

724

answers:

6

Hey I was wandering if you could help me with this. I have a website and I put the expire headers on all pages/img’s and scripts but I don’t kno how I could add expire headers to scripts not on my page. For example Google Analytics it has expire headers set to 1 day. But not Google is my problem. Some other adds from a website are the real problem. They don’t have expire headers at all.

I would be gratefull if someone could help. Thanks

+1  A: 

You can't.

Try e-mailing the one(s) hosting the file and try to get them to apply expires-headers to it.

Linus Sjögren
+4  A: 

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.

Gumbo
A: 

Thats not possible.

Not recommended (and not always possible): If its static content, prefetch it with a script and set your own headers.

echox
A: 

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');
JoostK
+1  A: 

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"&gt;&lt;/script&gt;

To

<script type="text/javascript" src="external.php?url=http://external.example.com/foo.js"&gt;&lt;/script&gt;

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.

raspi
A: 

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.

David Leston