Dynamic Sub Domain Using htaccess

Whether you are using Linux on the desktop or as a server, it's still good that you're using Linux. Linux related questions go here.

Moderator: General Moderators

lokesh7186
Forum Newbie
Posts: 3
Joined: Fri Aug 22, 2008 2:27 am
Contact:

Dynamic Sub Domain Using htaccess

Post by lokesh7186 »

Hi

I want to redirect the user from http://collegename.educationss.com to http://educationss.com/colleges/collegename .
What I am using currently is given below and works fine.

Code: Select all

 
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?educationss\.com$ [NC]
RewriteRule ^.*$ - [L]
RewriteCond %{HTTP_HOST} ^(.*?)\.(www\.)?educationss\.com$ [NC]
RewriteRule ^.*$ http://educationss.com/colleges/%1 [L]
 
Now I want that the user should be redirected but the URL should not change from http://collegename.educationss.com to http://educationss.com/colleges/collegename

An example can be seen http://csjmu.educationss.com/
Here the user is sent to http://educationss.com/colleges/csjmu
But I want the URL to remain http://csjmu.educationss.com/.

Thanks in advance
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Dynamic Sub Domain Using htaccess

Post by josh »

Are the document roots on different servers or the same server?
lokesh7186
Forum Newbie
Posts: 3
Joined: Fri Aug 22, 2008 2:27 am
Contact:

Re: Dynamic Sub Domain Using htaccess

Post by lokesh7186 »

the document roots on the same server.

PLease Help
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Dynamic Sub Domain Using htaccess

Post by josh »

Use virtual hosting not rewrites
lokesh7186
Forum Newbie
Posts: 3
Joined: Fri Aug 22, 2008 2:27 am
Contact:

Re: Dynamic Sub Domain Using htaccess

Post by lokesh7186 »

Actually what I want is that when a College comes and registers on my website, the college will automatically get a subdomain.

i.e. http://collegename.educationss.com

I create a directory for that college through my PHP code.

i.e. http://www.educationss.com/colleges/[b] ... _DIRECTORY[/b]

Now the above COLLEGE_DIRECTORY should work like http://COLLEGE_DIRECTORY.educationss.com

I thought that this is only possible through rewrite
If this is this possible with virtual hosting please let me know.

I will try to find out more about virtual hosting in the meantime and see if that is the correct way.

Thanks a lot and waiting for you Reply
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Dynamic Sub Domain Using htaccess

Post by VladSun »

++jshpro2
But maybe http://httpd.apache.org/docs/2.0/vhosts/mass.html would satisfy your needs better.
There are 10 types of people in this world, those who understand binary and those who don't
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Dynamic Sub Domain Using htaccess

Post by josh »

Yes, you create a virtual host that answers all subdomain reqeusts. and Handle the behavior you just described in the application layer with a front-end controller. That is the way I did it. You can set up conditional rewrites but are you going to create a physical document root for each "dynamic site"? That would be a pain
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Dynamic Sub Domain Using htaccess

Post by alex.barylski »

What wait a minute...you can setup sub-domains using Apache? I thought that part of the domain resolution process was handled by DNS still?

Example: sub.domain.com

.com is looked up and the registrar is the asked to return the DNS servers at which point the hosts DNS servers lookup the domain...and then Apache can handle the request for sub-domains???
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Dynamic Sub Domain Using htaccess

Post by josh »

In the DNS you can setup specific subdomains, or you can set wildcards ( create an A record for *.domain.tld) then in apache you can set up rules to catch requests using various methods ( virtual hosts, mod_rewrite, etc.. ) and map them to different document roots, or resources ( other machines, etc.. )

When you define a serverAlias you can just define *.foo.domain.tld to catch only 2nd level subdomains under the 1st level subdomain "foo", passing requests to _anything_.bar.domain.tld to the next apache virtual host, which might catch *.domain.tld for example, or even *.tld I think... or maybe not ( either way which would be a security risk )

You could have apache handle mapping to other machines to handle different subdomains too, mod_proxy I think... but if different subdomains map to different machines it would be better to catch it at the DNS level. You could even write your own custom DNS server if you were so inclined, to allow pragmatic logic in the domain to resource mapping rules
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Re: Dynamic Sub Domain Using htaccess

Post by Jenk »

Hockey wrote:What wait a minute...you can setup sub-domains using Apache? I thought that part of the domain resolution process was handled by DNS still?

Example: sub.domain.com

.com is looked up and the registrar is the asked to return the DNS servers at which point the hosts DNS servers lookup the domain...and then Apache can handle the request for sub-domains???
VHosts.

Code: Select all

<VirtualHost *:80>
  ServerName foo.myhost.com
  # do stuff for this vhost only
</VirtualHost>
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Dynamic Sub Domain Using htaccess

Post by alex.barylski »

OK Interesting...up until now I've had to ask my host to add the sub-domain record...but I can just ask them to setup an A record using a wildcard and use VHosts?

Ideally I want to setup sub-domains programatically from my end and not have to request for sub-domains via email each time a client signs up for a service...
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Dynamic Sub Domain Using htaccess

Post by josh »

Hockey wrote:just ask them to setup an A record using a wildcard and use VHosts?.
yep. Bind allows the wildcard, some control panels that some hosts use do not allow wildcards though, you can always host your own DNS
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Dynamic Sub Domain Using htaccess

Post by alex.barylski »

Hosting my own DNS would require two physically separate servers though, no?

In addition to the server powering my application that is a few hundred a month in fees. Ouch!
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Dynamic Sub Domain Using htaccess

Post by josh »

require no but recommended yes. you could even emulate it with 2 IPs on the same machine but you'd still be screwed if that 1 machine went offline
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Dynamic Sub Domain Using htaccess

Post by VladSun »

jshpro2 wrote:require no but recommended yes. you could even emulate it with 2 IPs on the same machine but you'd still be screwed if that 1 machine went offline
That's why it is also "recommended" that the 2 IPs are from different C-class subnets ;)
There are 10 types of people in this world, those who understand binary and those who don't
Post Reply