PHP Dynamic Subdomains

GeekThis happily runs on Vultr. Get $300 of free hosting credits to try out their cloud compute, kubernetes engine, or managed databases. Try Vultr today to claim your free $300.

Giving users a subdomain name is sometimes better when you want users to have their own piece of the website. For example, Tumblr, Blogger, WordPress all will give you a subdomain name. A subdomain is a prefix on the existing URL followed by a period. The most common sub domain you will see is “www”.

To give you users a subdomain is a little more difficult than just having the users profile as a directory on your site. You have to modify your web servers virtualhosts or host mapping, you also need to modify your DNS (sometimes).

Modify Web Server Configuration

The first part is to modify the web server configuration files. I’ll walk over how to do this in Nginx. We need to map all sub domain names to run the same block as the current website. Let’s say our current website is “example.com” We want user profiles to be “user1.example.com”, “user2.example.com” and so on.

Inside of the Nginx configuration file (nginx.conf), find the server block for the server_name of “example.com”. The configuration file varies on location based on operating system. If installed on Windows, it will be under the “conf” directory of the nginx installation location. If installed on linux, the file is probably under /usr/local/nginx/conf/

server {
	server_name  example.com;
	root /home/user/public_html/example.com
	index index.php index.html index.htm;
}

# Edit server_name to the following
# server_name example.com *.example.com

You will see that we added a space and then an asterisk (wildcard) followed by the domain name. This will map all subdomain names to use this server block as well.

Modify DNS Records

You now need to add a wildcard DNS record for all subdomain names of example.com. Depending on how you currently manage your DNS records, this will vary greatly on how you add the record, but the records will all be the same no matter what method you use.

The following record needs to be added (change main domain name though and the IP address). The first record maps all subdomain names to a specific IP address. The second record if used will map all subdomains to the main domain records, which will make future changes a lot easier.

*.example.com.   1200   IN   A   192.168.1.1
# OR
*.example.com.   1200   IN   CNAME   example.com

Modify DNS with CPanel

Since CPanel is one of the more popular hosting control panels, I felt that showing how to modify the DNS for CPanel would be useful to some. Instead of having to manually modify the DNS records in the “Advanced DNS Editor”, you are just create a new subdomain name.

  1. Open the “Subdomains” page / editor.
  2. Create a new “Subdomain” of “*” (asterisk). The path can be anything you want. I usually have all parts of the site point to the same directory and then have the index.php file manage what content to load.
  3. You have to wait a few hours for this to update. By default the TTL for subdomains are 14400 which is 4hours. You can open the “Advanced DNS Editor” and modify the TTL to 1200 which will be every 20 minutes if you are impatient.

Sample PHP Code

Now that all subdomains are mapped properly (and you waited for the DNS changes to take place), you can try the following PHP code to check what subdomain name is used. You will have to strip out the main address to get the subdomain name alone. You then can use the subdomain name to check the database for the specific content to show for that user or page.

  echo $_SERVER['HTTP_HOST'];

Related Posts

PHP - Check if Production or Sandbox

Use PHP to check of your site is on your development server or uploaded to the production server.

Setup PHP Development Web Server Quickly inside of Windows

Quickly setup a PHP development enviornment by using the built in PHP web server with no additional software required.

Parsing AWStats with PHP

Parse Awstat files using PHP including information about the awstats file format.

PHP Calculate Time Since

Calculate the difference between two times using PHP including example code.