I have urls like this:
And I want nginx to replace dots "." characters in the subdomain part of the hostname by dashes "-", so this would result in :
I already have a server block that catches the urls with dots in the "prefix" variable, but I could not find any documentation on how to rewrite url so that is replaces characters in "prefix" without using lua/...:
server {
listen 443 ssl;
listen [::]:443 ssl;
server_name "~^(?<prefix>[\w\.]+).somedomain.com$";
rewrite 302 "$scheme://What do i put in here?.somedomain.com$request_uri";
}
A big thanks for people that will try to help on this!
You could capture both sides of the . with separate captures, for example:
server {
listen 443 ssl;
listen [::]:443 ssl;
server_name ~^([^.]+)\.([^.]+)\.([^.]+)\.example\.com$;
...
return 302 $scheme://$1-$2-$3.example.com$request_uri;
}
server {
listen 443 ssl;
listen [::]:443 ssl;
server_name ~^([^.]+)\.([^.]+)\.example\.com$;
...
return 302 $scheme://$1-$2.example.com$request_uri;
}
Use a map to capture the subdomains:
map $host $resub {
~^([^.]+)\.([^.]+)\.somedomain\.com$ $1-$2;
~^([^.]+)\.([^.]+)\.([^.]+)\.somedomain\.com$ $1-$2-$3;
~^([^.]+)\.([^.]+)\.([^.]+)\.([^.]+)\.somedomain\.com$ $1-$2-$3-$4;
~^([^.]+)\.([^.]+)\.([^.]+)\.([^.]+)\.([^.]+)\.somedomain\.com$ $1-$2-$3-$4-$5;
# etc.
}
server {
server_name *.somedomain.com;
if ($resub) {
return 302 $scheme://$resub.somedomain.com$request_uri;
}
}