我的代码不适用于像domain.co.uk这样的二级tld
这是我的conf:
# add www.
if ($host ~ ^(?!www)) {
rewrite ^/(.*)$http://www.$host/$1 permanent;
}
# remove subdomain
if ($host ~ "^www\.(.*)\.(.*\.([a-z]{2,4}|[a-z]{2}\.[a-z]{2}))") {
set $host_without_sub $2;
rewrite ^/(.*)$http://www.$host_without_sub/$1 permanent;
}
编辑:
现在我正在使用它.它工作正常:
# rules
server {
#general
listen 80;
# add www.
if ($host ~ ^(?!www)) {
rewrite ^/(.*)$http://www.$host/$1 permanent;
}
# remove subdomain
if ($host ~ "^www\.(.*?)\.(.{3,}\.([a-z]{2}\.[a-z]{2}|[a-z]{2,4}))$") {
set $host_without_sub $2;
rewrite ^/(.*)$http://www.$host_without_sub/$1 permanent;
}
最佳答案