Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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

How do you get the subdomain as a variable to add in a rewrite? should go to but still display as just

I'm currently doing but would like to have Tumblr style domain names for users.

rewrite ^/users/(.*)$ /user.php?u=$1 last;

Also, if there are pages like /settings/ after the subdomain I'll need to rewrite to a settings.php

share|improve this question

Two things. Firstly you need to read the host variable to get the subdomain and set it to a variable which is used later.
I used the variable $sub_domain and and then append the variable to to the URI.

if ($host ~* (.*)\.example\.com) {
     set $sub_domain $1;
     rewrite ^/user.php$ /user.php?u=$sub_domain

The rewrite line needs to be tweaked further. I have given an example here. Refer to the nginx in-built variables documentation for available options.

share|improve this answer

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.