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

后端 未结 9 814
我寻月下人不归
我寻月下人不归 2020-12-25 14:03

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

相关标签:
9条回答
  • 2020-12-25 14:03

    You can't.

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

    0 讨论(0)
  • 2020-12-25 14:08

    Thats not possible.

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

    0 讨论(0)
  • 2020-12-25 14:09

    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.

    0 讨论(0)
  • 2020-12-25 14:15

    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.

    0 讨论(0)
  • 2020-12-25 14:16

    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');
    
    0 讨论(0)
  • 2020-12-25 14:23

    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.

    0 讨论(0)
提交回复
热议问题