How to create multiple level subdomains with apache2 and or PHP


Introduction

Subdomains are a great way to divide your website into different sections, making it easier for visitors to navigate and find what they’re looking for. But what if you need to create multiple levels of subdomains, such as blog.example.com or shop.blog.example.com? This can seem daunting, but with Apache2 and/or PHP, it’s actually quite simple. In this article, we’ll walk you through steps of creating multiple level subdomains with examples and sub-headings.

What are subdomains?

Subdomains are a way of creating a separate section of your website that has its own unique URL. For example, instead of having all of your content on www.example.com, you could create a subdomain called blog.example.com, where all of your blog posts are stored. This makes it easier for visitors to find content they’re interested in, and it can also be useful for SEO purposes.

Creating multiple level subdomains with Apache2

Apache2 is one of most popular web servers in use today, and it’s also very flexible when it comes to creating subdomains. Here’s how you can create multiple level subdomains with Apache2 −

  • Step 1 − Enable mod_rewrite

Before you can create multiple level subdomains with Apache2, you need to enable mod_rewrite. This is a module that allows you to rewrite URLs in a more user-friendly way. To enable mod_rewrite, open your Apache2 configuration file (usually located at /etc/apache2/apache2.conf) and add following line −

RewriteEngine On
  • Step 2 − Create a wildcard DNS record

Next, you need to create a wildcard DNS record for your domain. This will allow any subdomain that matches a certain pattern to be redirected to your website. To create a wildcard DNS record, log into your DNS provider and create a new record with an asterisk (*) as subdomain. Here’s an example −

*.example.com IN A 192.0.2.1

This will redirect any subdomain that matches *.example.com to your website.

  • Step 3 − Create a virtual host

Now it’s time to create a virtual host for your subdomain. Open your Apache2 configuration file again and add following lines −

<VirtualHost *:80> 
   ServerName example.com 
   ServerAlias *.example.com 
   DocumentRoot /var/www/html 
</VirtualHost>

This will create a virtual host for your main domain and any subdomain that matches *.example.com. DocumentRoot should point to directory where your website files are located.

  • Step 4 − Create a .htaccess file

Finally, you need to create a .htaccess file in root directory of your website. This file will tell Apache2 how to handle URL rewriting. Here’s an example −

RewriteCond %{HTTP_HOST} ^([a-z]+).example.com$ RewriteRule ^(.*)$ /%1/$1 [L]

This will redirect any subdomain that matches [a-z]+.example.com to a subdirectory with same name as subdomain. For example, blog.example.com would be redirected to /blog/.

Creating multiple level subdomains with PHP

If you’re using PHP, you can also create multiple level subdomains using $_SERVER['HTTP_HOST'] variable. Here’s how −

  • Step 1 − Get subdomain

First, you need to get subdomain from $_SERVER['HTTP_HOST'] variable. Here’s an example −

$host = explode('.', $_SERVER['HTTP_HOST']); $subdomain = $host[0];

This will split HTTP_HOST variable into an array, with each subdomain separated by a dot. first element of array will be subdomain.

  • Step 2 − Handle URL rewriting

Once you’ve got subdomain, you can use it to handle URL rewriting. Here’s an example −

if ($subdomain == 'blog') { 
   // Load blog page 
} 
elseif ($subdomain == 'shop') { 
   // Load shop page 
} 
else { 
   // Load default page 
}

This code checks subdomain and loads appropriate page based on its value. You can also use regular expressions to handle more complex subdomain patterns.

While creating multiple level subdomains with Apache2 and PHP is relatively easy, there are some best practices you should keep in mind to ensure that your website is secure and performs well.

Firstly, it’s important to remember that subdomains are treated as separate websites by search engines and web browsers. This means that you’ll need to configure each subdomain with its own SSL certificate if you want to use HTTPS. Additionally, you may need to configure cross-domain cookies if you’re using multiple subdomains to share data.

Another best practice is to keep your subdomains organized and easy to understand. For example, if you’re using subdomains for different products or services, it’s a good idea to use descriptive names like product1.example.com or service2.example.com. This will make it easier for visitors to understand what each subdomain represents and help with search engine optimization.

Finally, it’s important to keep your website’s performance in mind when using subdomains. Each subdomain will require its own set of resources, including DNS lookups, HTTP requests, and server processing. This means that using too many subdomains can slow down your website and negatively impact user experience. It’s a good idea to limit number of subdomains you use and optimize them for speed and performance.

Conclusion

Creating multiple level subdomains with Apache2 and/or PHP is a great way to organize your website and make it easier for visitors to navigate. By following steps outlined in this article, you can create subdomains like blog.example.com or shop.blog.example.com in just a few easy steps. Whether you’re using Apache2 or PHP, process is relatively simple and can be customized to fit your specific needs. So don’t be intimidated by subdomains – with a little bit of know-how, you can create a more user-friendly and organized website in no time.

Updated on: 14-Mar-2023

365 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements