开发者

The url of the images if I use a subdomain

开发者 https://www.devze.com 2023-03-12 03:28 出处:网络
I have a problem that is related to the URL of the images if I use a subdomain. For example, if the image is located at address

I have a problem that is related to the URL of the images if I use a subdomain. For example, if the image is located at address

http://www.mydomain.com/images/photo.jpg

And if I want to use subdomain to speed up page load, in this case subdom开发者_运维问答ain is:

http://img.mydomain.com

How should look like the url to the image foto.jpg if I use subdomain?

The path for subdomain:

/home/mydomain/public_html/img/

The path for images folder:

/home/mydomain/public_html/images/


just add an alias to your img subdomain and keep the same structure. This way if you need to change in the future it will be transparent

edit:

is also good to have more than one subdomain for assets. like img01 img02 and so on. But be these subdomains consistent.

like if you have the image image1.jpg that targets to

http://img01.domain.com/images/image1.jpg

the next time the imagen appears in the code should appear as

http://img01.domain.com/images/image1.jpg

AND NOT as

http://img02.domain.com/images/image1.jpg

so the cache can optimize the calls.

I usually use this formula

  return sprintf("http://img%02s.domain.com", abs(crc32($imagename) % (9)));

this way the subdomain will be always consistent with the image name

edit 2:

Browsers limit the amount of connection opens peer server. So by having multiple subdomains you are faking this and therefore improving the load speed of the page.

But in the other hand if for a same image you load it from more than one different server the browser's cache can't operate because it doesn't know that you want to load the same image.

So by hashing the image name you are obtaining always the same server and then obtaining the best of both worlds: more connections opens AND browser caching


If your subdomain's document root is:

/home/mydomain/public_html/img/

And your images at are:

/home/mydomain/public_html/images/

Then you will not be able to access them. You'd have to navigate to http://img.mydomain.com/../images/photo.jpg which, for very obvious security reasons, is not possible.

Your subdomain's document root should instead be:

/home/mydomain/public_html/images/

And then your URL is:

http://img.mydomain.com/photo.jpg
0

精彩评论

暂无评论...
验证码 换一张
取 消