简体   繁体   English

更改所有图像域的最简单方法是什么?

[英]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: 情况1:

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

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

case 2: 情况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? 由于我将所有图像都移到了CDN服务器,因此只需要更改img标签的基本URL,更改仅图像的基本域的最简单方法是什么? I don't what to edit each img in whole website. 我没有什么要编辑整个网站中的每个img。

You can use JavaScript like this: 您可以像这样使用JavaScript:

$('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. 这不是最佳方法,最好在服务器端执行此操作(如果可以的话),但这确实是工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM