CL So
CL So

Reputation: 3759

What is the simplest way to change the domain for all images?

This is the domain of my website

www.mydomain.com

And now I added new server, and moved all image to this server

cdn.mydomain.com

In my website, there are two cases.

case 1:

<head>
<base href="//www.mydomain.com">
</head>

<body>
<img src="img/internalImage.jpg">
<a href="internalLink.html">internal link</a>
</body>

case 2:

<head>
<base href="//www.mydomain.com">
</head>

<body>
<!--
Value of $row['url'] can be:
1. "img/relative.jpg"
2. "/img/absolute.jpg"
3. "//external.com/external.jpg"
-->
<img src="<?php echo $row['url']; ?>">
<a href="internalLink.html">internal link</a>
</body>

Since I moved all image to cdn server, so I only need to change base url for img tag only, what is the simplest way to change base domain for image only? I don't what to edit each img in whole website.

Upvotes: 1

Views: 866

Answers (1)

Mosh Feu
Mosh Feu

Reputation: 29277

You can use JavaScript like this:

$('img').attr('src', function(i, val) {
   return val.replace('www.mydomain.com', 'cdn.mydomain.com')
});

It's not the optimal way, better to do this in the server side (if you can) but it doe's the job.

Upvotes: 1

Related Questions