Page 1 of 2
Dynamic Sub Domain Using htaccess
Posted: Fri Aug 22, 2008 2:48 am
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
Re: Dynamic Sub Domain Using htaccess
Posted: Tue Sep 16, 2008 3:52 am
by josh
Are the document roots on different servers or the same server?
Re: Dynamic Sub Domain Using htaccess
Posted: Tue Sep 16, 2008 4:08 am
by lokesh7186
the document roots on the same server.
PLease Help
Re: Dynamic Sub Domain Using htaccess
Posted: Tue Sep 16, 2008 5:24 am
by josh
Use virtual hosting not rewrites
Re: Dynamic Sub Domain Using htaccess
Posted: Tue Sep 16, 2008 5:39 am
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
Re: Dynamic Sub Domain Using htaccess
Posted: Tue Sep 16, 2008 5:54 am
by VladSun
++jshpro2
But maybe
http://httpd.apache.org/docs/2.0/vhosts/mass.html would satisfy your needs better.
Re: Dynamic Sub Domain Using htaccess
Posted: Tue Sep 16, 2008 7:19 am
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
Re: Dynamic Sub Domain Using htaccess
Posted: Mon Sep 29, 2008 12:31 am
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???
Re: Dynamic Sub Domain Using htaccess
Posted: Mon Sep 29, 2008 3:21 am
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
Re: Dynamic Sub Domain Using htaccess
Posted: Mon Sep 29, 2008 8:58 am
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>
Re: Dynamic Sub Domain Using htaccess
Posted: Mon Sep 29, 2008 2:23 pm
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...
Re: Dynamic Sub Domain Using htaccess
Posted: Mon Sep 29, 2008 8:41 pm
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
Re: Dynamic Sub Domain Using htaccess
Posted: Tue Sep 30, 2008 1:24 pm
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!
Re: Dynamic Sub Domain Using htaccess
Posted: Tue Sep 30, 2008 2:29 pm
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
Re: Dynamic Sub Domain Using htaccess
Posted: Tue Sep 30, 2008 2:32 pm
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
