Redirect top-level subdomains to the first-level subdomain with nginx

0

I have an SSL certificate Wildcard and nginx. I want to redirect top-level subdomains to the first-level subdomain.

For example: If I have link I want to redirect to: link

    
asked by Harry Martel 24.11.2017 в 08:06
source

1 answer

1

You can use the following configuration in nginx:

server {
    listen 80;

    server_name  ~^(?<anyelse>.*)\.(?<subdomain>[\w-]+)\.com\.pe$;

    return  301  https://$subdomain.com.pe$request_uri;
}

server {
    listen 443;
    ssl on;
    server_name ~^(?<anyelse>.*)\.(?<subdomain>[\w-]+)\.com\.pe$;

    return      https://$subdomain.com.pe$request_uri;
}

server {
    listen 443;
    ssl on;
    server_name ~^(?<subdomain>[\w-]+)\.com\.pe$;

    add_header Strict-Transport-Security "max-age=31536000;" always;

    ssl_certificate /etc/nginx/ssl/cert.crt;
    ssl_certificate_key /etc/nginx/ssl/cert.key;

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    .
    .
    .
}
    
answered by 24.11.2017 в 08:06