tags:

views:

62

answers:

3

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?

+1  A: 

HTML

<base>

On subpages? If You don't have any static part of HTML, i dunno

Misiur
I'm guessing this would break all of the links..
Brendan Long
<base href="http://www.domain.com/" /> - and leave relative paths. Absolute won't be changed (I think).
Misiur
A: 

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

Peter Ajtai
+1, there's almost certainly a mod_rewrite solution that does not require rewriting every file by name, something like `RewriteRule ^imagedir/(.*) http://noCookies.example.com/$1`
bmb
That kind of defeats the point, doesn't it? The whole point to moving the images to a different domain is to reduce the HTTP requests hitting the server... And the point of Cookieless is to reduce http bandwidth. So you're killing both benefits...
ircmaxell
@bmb, thanks, I think you're closer to the working code, then my first attempt.@ircmaxell The main point of a cookie less domain is to speed up the user experience by not having the browser send a cookie along with the image request.... But yeah, you do have a point, since I believe the browser would still send the cookies?
Peter Ajtai
Well, using the rewrite engine, you're requiring 2 HTTP connections per file (one to the current domain, one to the cookieless). So you've just killed any possible gain in speed by avoiding to send the few hundred bytes to the other domain, and offset it by more than double...
ircmaxell
sed is good. Thx.
psychotik
@psychotik nice. yw!
Peter Ajtai
+1  A: 

You COULD (if you were lazy) do something like this:

At the start of the request (The top of the first file):

ob_start('parseImages');

Then declare the function:

function parseImages($data, $status) {
    static $body = '';
    switch ($status) {
        case PHP_OUTPUT_HANDLER_START:
        case PHP_OUTPUT_HANDLER_CONT:
            $body .= $data;
            return '';
            break;
        case PHP_OUTPUT_HANDLER_END:
            $body .= $data;
            $dom = new DomDocument();
            $dom->loadHtml($body);
            $imgs = $dom->getElementsByTagName('img');
            foreach ($imgs as $img) {
                $src = (string) $img->getAttribute('src');
                if (substr($src, 0, 4) != 'http') {
                    //internal link
                    $src = 'http://my.cookieless.com/' . ltrim($src, '/');
                    $img->setAttribute('src', $src);
                }
            }
            return $dom->saveHtml();
     }
}
ircmaxell