Dynamic Subdomains

ii silver ii

ǝɹıɟpǝʞɔı&
Dec 18, 2008
767
23
0
I'm trying to create dynamic sub domains for a test to see how they function, I know you need to create rewrite rules in .htaccess and also add a wildcard DNS entry but I'm struggling to get any of the methods working.

There's quite a few tutorials spread over google but most are outdated or not reliable.

Any help getting this set-up would be great thanks.
 


A way to test if you have a wildcard DNS setup correctly would be to try and ping blahblah.yourdomain.com

Then to check if php is getting the right subdomain, print_r($_SERVER);
 
Are you able to add a VirtualHost container in the apache config for *.thedomain.com ?

If so, keep in mind that the changes to httpd.conf and its included files won't be reflected until you restart apache.
 
Yes I can do this, any changes are possible.

Would someone mind just listing the changes to make the following change?

Code:
http://domain.com/test.php?username=bob

to

Code:
http://bob.domain.com
 
Yes I can do this, any changes are possible.

Would someone mind just listing the changes to make the following change?

Code:
http://domain.com/test.php?username=bob
to

Code:
http://bob.domain.com

just create a function that checks the current subdomain on page load. then you can render the data according to the subdomain, instead of rendering according to the GET string
 
Assuming wildcard DNS and Apache configs are set you could simply do mod_rewrite on the subdomain portion passing it onto the php (though php is capable of parsing request_uri and what not).

This is what I'm hoping to do but just trying to work out the settings, I'm getting a 404 at the moment and not sure whether it's the rewrite fail or wrong settings.

.htaccess
Code:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.domain\.com$ [NC]
RewriteRule ^$ http://www.domain.com/testSub.php?username=%2 [L]

Code:
NameVirtualHost 123.123.123.123

<VirtualHost 123.123.123.123>
    DocumentRoot /www/domain
    ServerName www.domain.com
    ServerAlias *.domain.com
</VirtualHost>

DNS Entry
Code:
*.domain.com. 14400 A 123.123.123.123