How to create multiple level subdomains with apache2 and or PHP

Subdomains are a powerful way to organize your website into distinct sections, making navigation easier for visitors. Creating multiple level subdomains like blog.example.com or shop.blog.example.com can be accomplished efficiently using Apache2 web server configuration and PHP scripting. This approach allows for flexible website architecture and improved content organization.

What are Subdomains?

Subdomains create separate sections of your website with unique URLs. Instead of placing all content on www.example.com, you can create dedicated areas like blog.example.com for articles or shop.example.com for e-commerce. This structure improves user experience, simplifies content management, and can enhance SEO by creating focused content areas.

Creating Multiple Level Subdomains with Apache2

Apache2 provides flexible subdomain management through virtual hosts and URL rewriting. Here's the step-by-step process:

Step 1 ? Enable mod_rewrite

Enable the mod_rewrite module to handle URL rewriting. Add this line to your Apache2 configuration file (typically /etc/apache2/apache2.conf):

RewriteEngine On

Step 2 ? Create Wildcard DNS Record

Set up a wildcard DNS record to redirect all subdomains to your server. In your DNS provider's control panel, create:

*.example.com IN A 192.0.2.1

This configuration directs any subdomain matching *.example.com to your web server.

Step 3 ? Configure Virtual Host

Create a virtual host configuration to handle subdomain requests:

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

This virtual host accepts requests for the main domain and all its subdomains, serving files from the specified document root.

Step 4 ? Create URL Rewrite Rules

Add a .htaccess file in your website's root directory to handle subdomain routing:

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

This rule redirects subdomains to corresponding directories. For example, blog.example.com routes to the /blog/ directory.

Creating Multiple Level Subdomains with PHP

PHP offers dynamic subdomain handling using the $_SERVER['HTTP_HOST'] variable for runtime subdomain detection and routing.

Step 1 ? Extract Subdomain

Parse the subdomain from the HTTP host header:

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

This code splits the host string by dots and captures the first segment as the subdomain identifier.

Step 2 ? Handle Dynamic Routing

Use conditional logic to route requests based on the detected subdomain:

if ($subdomain == 'blog') {
    include 'pages/blog.php';
} elseif ($subdomain == 'shop') {
    include 'pages/shop.php';
} else {
    include 'pages/home.php';
}

This approach allows dynamic content loading based on subdomain values, supporting complex routing patterns.

Best Practices

  • SSL Configuration ? Each subdomain requires separate SSL certificates for HTTPS functionality

  • Descriptive Naming ? Use clear subdomain names like api.example.com or admin.example.com for better organization

  • Performance Optimization ? Limit subdomain quantity to avoid excessive DNS lookups and server overhead

  • Cross-Domain Cookies ? Configure proper cookie domains for data sharing between subdomains

Security Considerations

Implement proper input validation when processing subdomain values in PHP to prevent security vulnerabilities. Use whitelist approaches for allowed subdomains and sanitize all user inputs before processing subdomain-based routing decisions.

Conclusion

Creating multiple level subdomains using Apache2 and PHP provides flexible website organization and improved user navigation. The combination of wildcard DNS records, virtual host configuration, and dynamic PHP routing creates a scalable subdomain architecture that adapts to growing website needs.

Updated on: 2026-03-17T09:01:38+05:30

660 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements