Wednesday, January 19, 2011

How do you do wildcard subdomains?

I want subdomain.example.com to point to example.com/profile.php?user=subdomain

With "subdomain" varying with user input

How do you do that? Also, I want it to be a silent redirect, so the subdomain.example.com is shown in the adress bar when the user types it or clicks on a link

  • See this one: http://www.easymodrewrite.com/example-subdomains You will actually need

    <IfModule mod_rewrite.c>
       Options +FollowSymLinks
       Options +Indexes
       RewriteEngine On
       RewriteBase /
       RewriteCond %{HTTP_HOST} !www.domain.com$ [NC]
       RewriteCond %{HTTP_HOST} ^(www.)?([a-z0-9-]+).domain.com [NC]
       RewriteRule (.*) domain.com/profile.php?user=%2 [L]
    </IfModule>
    

    EDIT: to have a "silent" redirect, the last rule should be:

       RewriteRule (.*) /profile.php?user=%2 [L]
    

    Make sure you have the profile.php in the directory where the DocumentRoot of the wildcard is set to.

    From naivists
  • I've not done this myself, but I'd start by:

    1. With your DNS provider, configure the wildcard.

      *.domain.com. IN A 1.2.3.4

    2. Use Apache's mod_rewrite to inspect the Host: header and modify the URL to match your specs.

  • The solution is probably a mix of all the mentioned solutions.

    First you will need the DNS entries as mentioned by JJ. Then you have to use the ServerAlias directive to make all subdomains pointing to the same directory. And at the end you could use the first 2 lines of Niek Bergman's code:

    $domain = explode(".", $_SERVER['SERVER_NAME']);
    $username = htmlentities($domain[0]);
    

    And from there on you can do with $username whatever you want.

0 comments:

Post a Comment