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

How do you get the subdomain as a variable to add in a rewrite?

hello.example.com should go to example.com/user.php?u=hello but still display as just hello.example.com

I'm currently doing http://example.com/users/hello 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 hello.example.com/settings/ I'll need to rewrite to a settings.php

share|improve this question

1 Answer 1

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

 
discard

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.