Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create subdomain upon user registration

I have a website where I want users that sign up to get their own subdomain. This subdomain is virtual, and every subdomain uses the same web server files.

I use PHP and Apache, and I know about Virtual Hosts, but I'm wondering where I need to put the vhosts code. First, I don't have access to httpd.conf. Second, I want this to be done automatically upon registration.

I've read about virtual hosts, but didn't find anything that answers my questions. Is there anyone who can explain me how all this works together, or know where I can find my answers?

like image 247
rebellion Avatar asked Dec 03 '09 16:12

rebellion


People also ask

Does a subdomain need to be registered?

Simple answer: No, you do not need to register a separate domain name for your subdomain. Depending on your domain name provider, there will be options to create additional subdomains.


1 Answers

Can you tell apache to read an extra .conf file? (traditionally you store your vhosts in httpd-vhosts.conf)

if so, add something like the following and restart your webserver

NameVirtualHost *:80

<VirtualHost *:80>
        DocumentRoot /abs/path/to/webroot
        ServerName   domainname.com
        ServerAlias *.domainname.com
        <Directory /abs/path/to/webroot>
                AllowOverride All
                Order allow,deny
                Allow from all
        </Directory>
</VirtualHost>

then in php, you can see which subdomain the user is requesting by inspecting:

$_SERVER['HTTP_HOST']

ie. if the user requests http://user1.domainname.com/index.php

$_SERVER['HTTP_HOST'] will have user1.domainname.com

you can then explode('.', $_SERVER['HTTP_HOST']) to inspect each segment.. etc.

like image 109
jlb Avatar answered Sep 22 '22 08:09

jlb