简体   繁体   中英

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?

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

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.

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM