1

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 subdomain 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/
Sergio
  • 1,207
  • 7
  • 28
  • 42
  • How exactly would it speed up page loads? Is the subdomain hosted on a different server? – Anirudh Ramanathan Jun 12 '11 at 18:55
  • 4
    @anirudh4444 Web browsers can be limited to 2 or 3 simultaneous web requests per host to play nice with web servers. By hosting your assets on multiple subdomains you're basically inviting the browser to hammer your server in one hit, making it load faster. – Tak Jun 12 '11 at 18:59
  • It speeds up page load times, because web browsers only allow X connections per domain. So, if you put your images on a subdomain, you can achieve 2X connections, thus a speedier experience per browser. – Brian Webster Jun 12 '11 at 18:59

2 Answers2

8

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

Community
  • 1
  • 1
Gabriel Sosa
  • 7,897
  • 4
  • 38
  • 48
  • 1
    don't say that. Every day is a nice day to learn something new. Check my edits. Saludos – Gabriel Sosa Jun 12 '11 at 19:12
  • it doesnt work for me. subdomains should have different ip adresses? – PiKey Apr 11 '13 at 06:45
  • @PiKey not required. you can have simple CNAMES targeting to the same `backend` host. This is only an `illusion` for the browser. but if you are using apache remember adding `ServerAlias` for each hostname to apache reply to the requests – Gabriel Sosa Apr 11 '13 at 19:56
3

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
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • But if I create subdomain using Cpanel it will create new folder with the same name as subdomain. How can I create subdomain for existing folder? – Sergio Jun 12 '11 at 19:10
  • @Sergio: So you're changing this to a Server Administration question now? – Lightness Races in Orbit Jun 12 '11 at 19:11
  • I'm not changing anything, the question remains the same. Just asking how can I do that. – Sergio Jun 12 '11 at 19:14
  • If you can't change the server configuration, the only option is to copy the images to the other folder or us an alias as Gabriel said. There is no other way. – Gerben Jun 12 '11 at 20:04
  • URL Rewrite? But I guess it will not be that efficient for images or other static resources... – LazyOne Jun 12 '11 at 20:07