I'm trying to find the right regex to rewrite all subdomains to www.
so for example w7w.domain.com to www.domain.com
or alskdaslkdja.domain.com to www.domain.com
i've tried many things, my last attempt was:
if ($host ~* (?!www\.)(.*))
{
set $host_without_www www.$1;
rewrite ^(.*)$ http://$host_without_www$1 permanent;
}
but that didn't work either.
i need to catch these and can't just do a wildcart rewrite to the www.domain.com because i have several domains being served on this instance.
any ideas?
server {
listen xx.xx.xx.xx:80;
server_name www.example.com;
# ....
}
server {
listen xx.xx.xx.xx:80;
server_name example.com *.example.com;
rewrite ^ http://www.example.com$request_uri? permanent;
}
server {
# Prefix server wildcards are checked before regexes, so this
# will catch anything starting with www.
server_name www.*;
}
server {
# This should redirect anything that didn't match the first
# server to domain.tld
# I think this regex will capture the domain.tld
server_name ~(?<domain>[^.]+\.[^.]+)$
rewrite ^ http://www.$domain$request_uri? permanent;
}
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.