douxun7992 2010-07-15 18:52
浏览 44
已采纳

将所有img链接更改为指向PHP中的无Cookie域的理想方式

I have a PHP site using MVC, which builds HTML dynamically for most requests. I'm updating my site to host images/static content on a cookieless domain. Currently the images/css are written out as links to relative URLs.

The best I can think of for now is to change all html that writes out <img> tags and css links to use a PHP function which inserts an absolute URL with the cookieless domain instead of the relative URL. However, this involves a lot of change to code and there is potential to miss a few tags/links.

Any suggestions on a better way to handle this?

  • 写回答

3条回答 默认 最新

  • dssk35460 2010-07-15 19:06
    关注

    The sed solution:

    Use sed on all you html files. You can loop through all files with PHP (or a script function). glob is a great function for this.

    <?php
    // this sed assumes you have 
    // <img src="imgs/example.png" />
    // and want to change it to
    // <img src="http://www.noCookies.com/imgs/example.png" />
    
    // Loop through each .html file in current directory and create an .html2
    // If satisfied w results can finalize .html2 files
    // I use .html2 so you don't overwrite your original files
    
    foreach (glob("*.html") as $file)
    {        
        // You have to escape double quotes
        $lookFor = 'src=\"imgs';
        $replaceWith = 'src=\"http://www.noCookies.com/imgs';
    
        // create the sed command:
        // _g is to do a global search
        $shellCommand = "sed s_{$lookFor}_{$replaceWith}_g {$file}";
    
        // create a NEW file and open it
        $fp = fopen("{$file}2", "w");
    
        // preform sed and write it to the new file
        fwrite($fp, shell_exec($shellCommand));
        fclose($fp);
    }
    ?>
    

    Edit - original answer follows: Another option option, which is probably just as slow or slower than the original is mod rewrite

    If you run Apache, and all the images are in a separate folders (even if they're not, but then it's more work), you could use mod_rewrite... something like this...? I'm a little rusty oon mod_rewrite, but you want to take the relative path, and switch it with an absolute path:

    RewriteEngine on
    RewriteRule ^media/images/(.*) http://noCookies.com/media/images/$1 [L]
    

    I think it's definitely the way the go though. Then you can change the old img sources slowly over time. Or just keep the old ones and add in the correct img src for any new images you add.

    Here is the mod_rewrite documenation, which is a little opaque... Some of the tutorials on preventing image hotlinking could also be helpful

    I just remembered, this is a good mod_rewrite tips and tricks page

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥100 高价邀请复制 域天d8联网狗
  • ¥15 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 蓝桥杯单片机第十三届第一场,整点继电器吸合,5s后断开出现了问题
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?