I have this domain structure and need make some redirect like:
sub1.sub2.sub3.domain.com/path -> sub2.sub3.domain.com/path
sub2.sub3.domain.com/path -> sub3.domain.com/path
sub1.domain1.com/path1 -> domain1.com/path1
sub2.domain1.com/path2 -> domain1.com/path2
sub3.sub4.domain1.com/path -> sub4.domain1.com/path
sub4.domain.uk.co/path -> sub4.domain1.uk.co/path
sub5.domain6.com/path3 -> domain6.com/path3
sub7.domain6.com/path4 -> domain6.com/path4
sub8.sub8.domain9.com/path5 -> sub8.domain9.com/path5
www.domain9.com/path5 -> domain9.com/path5
"domain.com" "domain6.com" "domain9.com" "domain1.uk.co" isn't host in this server.
I need redirect without hardcode any domain name. I try this:
if ( $host ~ ^www\.(.+)$ ) {
set $without_www $1;
rewrite ^ $scheme://$without_www$uri permanent;
}
But I need hadcoding "www" (and subXXX). How can I get the part before the point and dispose at the new address?
It's a bit late to answer, in the end with Ivan's help I made this regular expression. I defined a site with it if a request falls on this site, nginx will remove the subdomain.
if ($host ~ "^[^.]+\.(?<domain>[^.]+\..+)$") {
rewrite ^ $scheme://$domain$request_uri permanent;
}