Recommended:

  • phpclasses.org
  • jsclasses.org
  • jsmag.com
  • siteapps.com
  • View our reviews on Hot Scripts
  • JS Tutorial
  • scripts.com
  • securesignup.com




Recent Comments

Powered by Disqus




Back to articles

Dynamic sub-domains

Browsers usually have limit on how many files they can download from one domain simultaneously, so in theory, if you files would be on different domains or sub-domains, they will be downloaded simultaneously, thus loading your page faster. Of course you don't want to create different sub-domains for different files and you don't have to. There is an other way to do it.

It is called dynamic sub-domains, it is pretty hard to set up, but it is claimed to make your site load up to 5 times faster.

Most common use of dynamic sub-domains is getting images, css and javascript files from different sub-domains. You can put each type of files in separate directories and then use mod rewrite to access them.

Using domain.com as an example this is how it would look. Images are in domain.com/images, css files are in domain.com/css folder and javascript files are in domain.com/js folder. So we have image bg.png and it is located in domain.com/images/bg.png, but in HTMl code we refer to it as i.domain.com/bg.png.

To do that, you'll need to set up dynamic sub-domain in DNS and Apache. Edit your BIND settings by adding new line * IN A your.ip.address, then in  apache configuration (httpd.conf) add ServerAlias *.domain.com to your virtual host.

And last thing is to create .htaccess file and put this code in it:

# redirect externally if path doesn’t match host name
RewriteCond %{HTTP_HOST} !^i.domain.com$
RewriteRule ^images/([^/]+)$ http://i.domain.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} !^j.domain.com$
RewriteRule ^js/([^/]+)$ http://j.domain.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} !^c.domain.com$
RewriteRule ^css/([^/]+)$ http://c.domain.com/$1 [L,R=301]

# redirect internally to the file
RewriteCond %{HTTP_HOST} ^i.domain.com$
RewriteRule !^images/ images%{REQUEST_URI} [L]
RewriteCond %{HTTP_HOST} ^j.domain.com$
RewriteRule !^js/ js%{REQUEST_URI} [L]
RewriteCond %{HTTP_HOST} ^c.domain.com$
RewriteRule !^css/ css%{REQUEST_URI} [L]


This will do the following:

http://domain.com/images/bar externally to http://i.domain.com/bar
http://i.domain.com/bar internally to /images/bar

http://domain.com/css/foo externally to http://c.domain.com/foo
http://c.domain.com/foo internally to /css/foo

http://domain.com/js/bar externally to http://j.domain.com/bar
http://j.domain.com/bar internally to /js/bar

As well as correcting mismatching paths and host names for example:
http://i.domain.com/css/foo externally to http://c.domain.com/foo
http://c.domain.com/images/bar externally to http://i.domain.com/bar


You may also be interested in:

Powered by BlogAlike.com

blog comments powered by Disqus