nginx subdomain rewrite

2020-02-05 03:48发布

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?

3条回答
可以哭但决不认输i
2楼-- · 2020-02-05 04:25
rewrite ^/list-c-([0-9]+)-k-(.+)-o-1\.html$ /index.php?module=Default&action=List&c=$1&k=$2&o=1 last;
rewrite ^/list-c-([0-9]+)-k-(.+)-o-1-p-(.+)\.html$ /index.php?module=Default&action=List&c=$1&k=$2&o=1&p=$3 last;
rewrite ^/list-c-([0-9]+)-k-(.+)-o-1-price-(.+)\.html$ /index.php?module=Default&action=List&c=$1&k=$2&o=1&price=$3 last;
rewrite ^/list-c-([0-9]+)-k-(.+)-o-(.+)-p-1\.html$ /index.php?module=Default&action=List&c=$1&k=$2&o=$3&p=1 last;
rewrite ^/list-c-([0-9]+)-k-(.+)-o-(.+)-p-(.+)\.html$ /index.php?module=Default&action=List&c=$1&k=$2&o=$3&p=$4 last;
查看更多
别忘想泡老子
3楼-- · 2020-02-05 04:27

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 {
#    ....
#  }
}
查看更多
Summer. ? 凉城
4楼-- · 2020-02-05 04:42

If it's possible I'd only create 1 server(virtual host) which is the normal domain.com/www.domain.com and then use the conf to rewrite the rest to them

server {
    server_name domain.com www.domain.com;
    # normal handling for files
}

server {
    server_name ~(?<subdomain>[^\.]*).domain.com;
    location / {
        try_files keyword.php?keyword=$subdomain =404;
    }
}

please tell me if I missed something.

查看更多
登录 后发表回答