Webmasters Stack Exchange is a question and answer site for pro webmasters. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Sorry if this has been covered, I can't find anything on this specifically.

I have wildcard subdomains on (*.mysite.com) I need a mod_rewrite expression for this rewrite:

bob.mysite.com => mysite.com/users/index.php?user=bob

bob.mysite.com/profile/ => mysite.com/users/index.php?user=bob/profile/

Obviously "bob" and "profile" are just examples, I need the general case. Thanks for your help!

share|improve this question

migrated from serverfault.com May 19 '11 at 1:55

This question came from our site for system and network administrators.

up vote 3 down vote accepted

The $ operator will let you extract backreferences from matches in rewrite rules and the % operator will let you extract references from conditions.

RewriteCond %{HTTP_HOST} !www.mysite.com$ [NC] # Presuming you don't want to do www
RewriteCond %{HTTP_HOST} ^(.*)\.mysite\.com [NC] # Catch subdomain
RewriteCond %{REQUEST_URI} !index\.php [NC] # Don't rewrite if we already have
RewriteRule ^(.*)$ /users/index.php?user=%1$1 [L]
share|improve this answer
this almost works, except the $1 backreference returns the new address /users/index.php instead of the requested /profile/. Can you help? – user745668 May 25 '11 at 1:13
hmm... yeah, it will be rewriting it twice. Try with the edit included above. – Charles Keepax May 25 '11 at 7:59

Your Answer


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

Not the answer you're looking for? Browse other questions tagged or ask your own question.