Replace url() relative path with full domain in css files

Posted by deepwell on Stack Overflow See other posts from Stack Overflow or by deepwell
Published on 2011-01-14T19:18:15Z Indexed on 2011/01/14 22:53 UTC
Read the original article Hit count: 97

Filed under:
|
|

I'd like to run a script on release that replaces all url() declarations in a css file with the full domain path, because images are hosted on a static web server.

Example

Current:  background-image: url(/images/menu.gif);
Desired:  background-image: url(http://example.com/images/menu.gif);
Current:  background-image: url('/images/menu.gif');
Desired:  background-image: url('http://example.com/images/menu.gif');
Current:  background-image: url("/images/menu.gif");
Desired:  background-image: url("http://example.com/images/menu.gif");

I have concocted a bash script using sed to do just that, but it does not handle url with quotes url(''), or urls that already have a full path.

 STATIC_HOST="http://example.com"
 sed -i '' "s|url(\([^)]*\)|url($STATIC_HOST\1|g" main.css

© Stack Overflow or respective owner

Related posts about css

Related posts about bash