Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Ravidev's avatar

Wildcard DNS

How to have subdomain for each newly created user..(Wildcard DNS) dynamically through programing(php) ..Does any of larcasts series have covered this.. If so, plz provide series name.

0 likes
2 replies
LaryAI's avatar
Level 58

To achieve wildcard DNS for subdomains dynamically through programming in PHP, you need to configure both your DNS settings and your web server. Here's a step-by-step guide:

Step 1: Configure Wildcard DNS

  1. DNS Provider Configuration:
    • Log in to your DNS provider's dashboard.
    • Add a new DNS record:
      • Type: A (or CNAME if you're pointing to another domain)
      • Name: * (this is the wildcard part)
      • Value: Your server's IP address (or the domain if using CNAME)

This configuration will direct any subdomain requests to your server.

Step 2: Web Server Configuration

  1. Apache Configuration:

    • Ensure your Apache configuration allows for wildcard subdomains. You can do this by editing your virtual host file (e.g., /etc/apache2/sites-available/000-default.conf):
    <VirtualHost *:80>
        ServerName example.com
        ServerAlias *.example.com
        DocumentRoot /var/www/html
        # Other configurations...
    </VirtualHost>
    
    • Restart Apache to apply changes:
    sudo systemctl restart apache2
    
  2. Nginx Configuration:

    • Edit your Nginx server block (e.g., /etc/nginx/sites-available/default):
    server {
        listen 80;
        server_name example.com *.example.com;
        root /var/www/html;
        # Other configurations...
    }
    
    • Restart Nginx to apply changes:
    sudo systemctl restart nginx
    

Step 3: PHP Handling

  1. PHP Script:
    • In your PHP application, you can capture the subdomain and handle it accordingly. For example:
    <?php
    $host = $_SERVER['HTTP_HOST'];
    $subdomain = explode('.', $host)[0];
    
    // Assuming your main domain is example.com
    if ($subdomain !== 'example') {
        // Handle subdomain logic, e.g., load user-specific data
        echo "Welcome, user from subdomain: " . htmlspecialchars($subdomain);
    } else {
        echo "Welcome to the main site.";
    }
    ?>
    

Additional Considerations

  • Security: Ensure that your application properly sanitizes and validates subdomain inputs to prevent security vulnerabilities.
  • SSL Certificates: If you're using HTTPS, consider using a wildcard SSL certificate to cover all subdomains.

As for Laracasts series, while there might not be a specific series dedicated solely to wildcard DNS, you can find relevant information in series related to server management or PHP application deployment. Check out series like "Laravel Forge" or "Server Management" for more insights on server configurations.

renato.hysa.dev's avatar
Level 2

This is the nginx conf you can use:

1 like

Please or to participate in this conversation.