Php – wilcard DNS and Virtual subdomains

PHPsubdomainvirtualwildcard-subdomain

I just don't get it..

I'm planning to get scenario like this: username.mydomain.com = mydomain.com/user.php?user=username

Well, let's not go that far yet because this wildcard DNS is making me insane!! :/

I have DNS record

* A my-ip-address

And I have in my vhost file

ServerAlias *.mydomain.com

But it just doesn't work at all.. No wait, example test.mydomain.com is working but not any random subdomain.. Wut?

When I ping any subdomain it find my ip like it should be. Can anyone help me?

Okey and then this other issue. When (and if) I'll get this virtual subdomain working, what would be best sollution to make it work like I explained above?

username.mydomain.com equals to mydomain.com/user.php?user=username

but user can see it just like it would be subdomain… Little lost here, sry! 🙂

Thanks for all help!

Best Answer

just dealt with the same exact situation

what you need to do is

  • go to Advanced DNS Zone Editor in cpanel
  • select the domain
  • name : *.doaminname.com
  • TTL : 14400
  • Type: CNAME
  • Address : domainname.com/path
  • So, it would be like: *.doaminname.com CNAME 14400 domainname.com/path

and then a little tweak in .htaccess

# Extract the subdomain part of domain.com
RewriteCond %{HTTP_HOST} ^([^\.]+)\.domainname\.com$ [NC]

# Check that the subdomain part is not www and ftp and mail
RewriteCond %1 !^(www|ftp|mail)$ [NC]

# Redirect all requests to a php script passing as argument the subdomain
RewriteRule ^.*$ http://www.domainname.com/path/%1 [R,L]

here %1 is the subdomain bit.

hope, this helps some body other lost in this case. thanks.

Related Topic