Different subdomains using common scripts?

They have: 12 posts

Joined: Jun 2006

Hi, you clever people.

As the title says, I'd like to be able to use one collection of (Perl) scripts to run three or four different subdomains.

Also, I'd like the scripts to be able to read the domain name and adjust its output accordingly.

Put another way, I'd like to use

xyz.mydomain.com/somescript.cgi

instead of

mydomain.com/somescript.cgi?somevariable=xyz

I can obviously duplicate the scripts, but that's not very neat, and it's a real pain when I edit them.

Thanks! Smiling

Greg K's picture

He has: 2,145 posts

Joined: Nov 2003

Will each subdomain have its own site, or will they all be generated off of the perl scripts based on the subdomain?

If they are only getting the site from the perl scripts, i think I know a way.

-Greg

They have: 12 posts

Joined: Jun 2006

Not sure exactly what you're getting at, but I'll try to explain. The subdomains will each appear to be a different, independent site, but there will be one set of scripts. The scripts will be in the server's public_html directory (but made executable via the .htaccess file). My hosting company allows me to set up subdomains, e.g. xyz.mysite.com, but the files for this would normally be placed in a folder 'xyz' (or whatever) which has the same level of hierarchy as public_html. xyz.mysite.com/somescript.cgi would normally try to access the file somescript.cgi within the 'xyz' folder.

I suppose I'm after some command that instructs xyz.mysite.com/somescript.cgi to execute somescript.cgi within public_html, rather than within xyz. I suspect it should be reasonably straightforward for that script to tell which subdomain is accessing it by looking at the environment variables. Of course, I will still want links generated by somescript.cgi to point to xyz.mysite.com/someotherscript.cgi, rather than mysite.com/someotherscript.cgi, otherwise the subdomain information will be lost.

I think I'm confusing myself, as usual. Does that make any kind of sense?

Thanks!

mr cgi guy's picture

He has: 5 posts

Joined: Oct 2006

how about mod_rewrite?
you'd need to have an .htaccess file in each directory, but you wouldn't need to duplicate the script then.
would be pretty easy to capture the subdomain value then without having to rely on env variables.

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

Symbolic linking on a *nix platform is also an option.

But mod_rewrite would be the prefered method.

Depending on how your server/config acts, you could opt to not create subdomains (in apache). At this point, all sub-domains should pull up the same site. Then, deploy an .htaccess file in the document_root of your domain, which captures the prefixed subdomain and appends it as part of a querystring.

Mark Hensler
If there is no answer on Google, then there is no question.

They have: 12 posts

Joined: Jun 2006

Thanks guys, I'll read up on mod_rewrite.
Nice to see it's unanimous!

They have: 12 posts

Joined: Jun 2006

Hi all - I finally had the time and courage to try it, but I have a real annoyance.

mod_rewrite seems to have a problem - it's easy enough to do a rewrite as long as I stay within a certain subdomain, but if I want to refer a script from (for example) http://foo.bar.com/script.cgi to http://bar.com/script.cgi?subdomain=foo, it has to go as a redirect rather than a rewrite; in other words, the new URL appears in the address bar (and I have to write the domain explicitly in the RewriteRule).

Am I stuck with this?

I found something somewhere saying that the [P] tag at the end of the RewriteRule is what I want (proxy???), but it just causes an error (maybe I don't have mod_proxy?)

Hope you can make sense of this gibberish!

They have: 15 posts

Joined: Dec 2006

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^\.]+)\.mydomain\.com$ [NC]
RewriteRule ^(.*)$ somescript.cgi?somevariable=%1 [L]

Try that, obviously replacing the bold values with your own site and page. The above works for me on one of my sites.

There is once thing you will need though: You will need to have a dedicated IP given to you by your host. Ask them and they will give you one at a small cost.

Let me know how you get on.

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.