Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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

share|improve this question

migrated from superuser.com Jan 3 '10 at 22:59

This question came from our site for computer enthusiasts and power users.

3 Answers 3

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]

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.

share|improve this answer
Thanks, that sort of worked except when I type in the URL it does a physical redirect instead of a silent one What I want is for the subdomain to remain in the address bar so the users won't know theres a redirect –  Ryan Jan 3 '10 at 16:34
@Ryan, then you can call the "profile.php" from your subdomain, e.g. make http://user1.domain.com equal to calling http://user1.domain.com/profile.php?user=user1 Edited my post to describe the solution. –  naivists Jan 3 '10 at 16:51

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

  1. With your DNS provider, configure the wildcard.

    *.domain.com. IN A

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

share|improve this answer

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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.