Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nginx subdomain rewrite

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?

like image 383
ivan73 Avatar asked Mar 23 '10 09:03

ivan73


1 Answers

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 {
#    ....
#  }
}
like image 104
Alexander Azarov Avatar answered Oct 14 '22 22:10

Alexander Azarov