DanTdr
DanTdr

Reputation: 424

How can I add expire headers for scripts that are not on my server?

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.

Upvotes: 20

Views: 52774

Answers (9)

Avni Yayin
Avni Yayin

Reputation: 469

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.

Upvotes: 1

CoderCat
CoderCat

Reputation: 27

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

Upvotes: 1

Php Dev
Php Dev

Reputation: 59

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"

Upvotes: -4

David Leston
David Leston

Reputation:

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.

Upvotes: -10

raspi
raspi

Reputation: 6122

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.

Upvotes: 21

Joost
Joost

Reputation: 10413

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');

Upvotes: 2

echox
echox

Reputation: 9860

Thats not possible.

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

Upvotes: 2

Gumbo
Gumbo

Reputation: 655239

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.

Upvotes: 22

Linus
Linus

Reputation: 622

You can't.

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

Upvotes: 0

Related Questions