views:

529

answers:

1

I need a nginx rewrite rule to rewrite from:

http://some-keyword.example.com to www.example.com/keyword.php?keyword=$some-keyword

while domain without www in front still rewrites to www.example.com and www isn't taken as a keyword.

Please could you help me to solve this problem, how to write these two rules?

+1  A: 

If you meant redirect, then:

server {
  server_name ~^(.*)\.example\.com$ ;

  rewrite ^ http://www.example.com/keyword.php?keyword=$1 redirect;
}

In the case of rewrite then simply do

server {
  server_name example.com ~^(.*)\.example\.com$ ;

  rewrite ^ /keyword.php?keyword=$1 break;

#  location /keyword.php {
#    ....
#  }
}
Alexander Azarov
Even if the question was regarding rewrites, your example does not exclude "www" like it should. You need to add a negative lookahead construct for that: ` ^((?!www\.).*)\.example\.com$ `
conny
The config should be ok without negative lookahead, because Nginx has server search ordering such that full names (www.example.com) go before regexes.
Alexander Azarov