Mapping subdomains to parametered web pages in Apache


#1

Mapping john.webserver.com to http://www.webserver.com/user.php?name=john
is just by changing a few parameters in http.conf file.

<VirtualHost se.rv.er.ip.ad.re.ss>
        ServerAdmin [email protected]
        DocumentRoot /var/www
        ErrorLog /var/log/apache/apache-error.log
        TransferLog /var/log/apache/apache.log
        ServerName www.servername.com
        ServerAlias *.servername.com
        RewriteEngine on
        RewriteCond %{HTTP_HOST} !^www.* [NC]
        RewriteCond %{HTTP_HOST} ^([^\.]+)\.servername\.com
        RewriteRule ^/$ http://www.servername.com/user.php?user=%1
</VirtualHost>

The lines for this feature,

RewriteEngine on activating rewrite option

RewriteCond %{HTTP_HOST} !^www.* [NC] the rule will not be applied to subdomain “www”

RewriteCond %{HTTP_HOST} ^([^\.]+)\.servername\.com subdomain section will be our parameter

RewriteRule ^/$ http://www.servername.com/user.php?user=%1 parameter section which is grabbed as subdomain name will be our value for user variable to user.php file.