Redirecting all subdomains to www in nginx with virtual hosting |
June 17th, 2011 |
| nginx, tech |
server_name ~^(?<subdomains>.+\.)?(?<domain>[^.]+\.[^.]+)$;
if ($subdomains != www.) {
rewrite ^/(.*)$ http://www.$domain/$1 permanent;
}
This will do:
- www.example.com: no change
- example.com: www.example.com
- mail.example.com -> www.example.com
- mail.example2.com -> www.example2.com
- foo.bar.baz.example.com -> www.examplecom
Comment via: substack