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?
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.
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:
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