Forum Moderators: phranque

Message Too Old, No Replies

How to call a JavaScript file from a subdomain

         

crosescu

8:19 am on Dec 13, 2009 (gmt 0)

10+ Year Member



I have a website with a number of subdomains that are all hosted on the same server.
On the root domain I have a JavaScript file that is used by all the subdomains and I like to keep the JavaScript file on the root domain rather than coping it to every subdomain.

How can a subdomain load the javascript file using a relative path to avoid doing Ajax calls on a cross domain?

Is it possible to do an Internal Rewrite in Apache .htaccess file like below?
RewriteRule ^base/(.+)?$ /path_to_root/$1 [L]

where the short url "/base/test.js" is handled by the Rewrite Rule above

Thank you for your help in advance

jdMorgan

12:52 am on Dec 14, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



All I can say is "go ahead and try it," since the answer depends on your server configuration.

If your subdomains are all mapped to the same DocumentRoot as your main domain, and you then use a rewriterule to 'map' the subdomain requests into subfolders as separate filespace as needed, then this approach will work.

However, if the subdomains have separate DocumentRoots --either completely-separate or in subdirectories below your main domain's DocumentRoot-- then it won't be possible to take an incoming HTTP request and rewrite it "up" in the directory-space (This can't be allowed because it would be a huge security risk if the domains/subdomains were separately owned or controlled).

So in this case, you'll need to look into using mod_alias Alias or AliasMatch directives to re-map the requests from the server config level, or alternatively, to create symlinks in each of the subdomains to point test.js requests arriving for that subdomain over to some common filespace containing the 'real' file. With the right file permissions settings, this should be possible.

Jim

crosescu

11:48 pm on Dec 15, 2009 (gmt 0)

10+ Year Member



mod_alias Alias did the trick. thank you for your help