Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 722
3 revs, 2 users 80% Avatar asked Sep 17 '09 13:09

3 revs, 2 users 80%


People also ask

How do I add expired headers?

Place all of the resources (such as images, scripts, etc.) that you would like to set far-future expirations for into the static folder and then add an . htaccess file to that folder that includes the expires headers. Place the rest of the resources that you do not want cached into the other folder .

Should I add expired headers?

Adding Expires Headers is important to reduce HTTP requests which reduces the time it take for the server to communicate with the browser. It also allows your users to reuse the cache files that have been stored in the browser to reduce the amount of files they need to download.

How do I add expired headers in IIS?

To configure an Expires header in IIS, right-click the Default Web Site in the Computer Management Console, select Properties, then click the HTTP Headers tab, which Figure 4 shows. Select the Enable Content Expiration check box and choose your expiration preferences.

HOW use expired header in HTML?

The expires header is an HTTP header that indicates whether it is possible to obtain information on request from the browser cache or if you need to access the server since the page option in the cache is already outdated. This header contains the date and time until the page is available in the browser cache.


2 Answers

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.

like image 80
Gumbo Avatar answered Sep 28 '22 01:09

Gumbo


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.

like image 43
raspi Avatar answered Sep 28 '22 03:09

raspi