开发者

nginx subdomain rewrite

开发者 https://www.devze.com 2022-12-23 16:27 出处:网络
I need a nginx rewrite rule to rewrite from: http://s开发者_开发知识库ome-keyword.example.com to www.example.com/keyword.php?keyword=$some-keyword

I need a nginx rewrite rule to rewrite from:

http://s开发者_开发知识库ome-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?


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 {
#    ....
#  }
}


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;


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消