www and without www for a folder and any other subdomain for another folder

Asked

Viewed 215 times

1

I have a domain configured in Nginx as follows:

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    server_name   .dominio.com.br ~^(www\.)?(?<domain>.+)$;
    root /home/dominio.com.br/public;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }


    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
    }

    location ~ /\.ht {
        deny all;
    }
}

This makes any of these links work:

dominio.com.br Subdomain.dominio.com.br

If I configure a DNS of another domain pointing to the IP of this my server the app also works because of that line:

server_name   .dominio.com.br ~^(www\.)?(?<domain>.+)$;

I wish I could direct to another folder if the link is:

www.dominio.com.br dominio.com.br

That is, any subdomain (except www) must go to a folder already the subdomain www and without www go to another folder.

I tried creating a new server block {} as in the example below, but it didn’t work:

server {
    listen 80;
    listen [::]:80;

    server_name   dominio.com.br www.dominio.com.br;
    root /home/outra_pasta/public;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }


    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
    }

    location ~ /\.ht {
        deny all;
    }
}

How can I resolve this issue?

  • Which block is being loaded first? Are they inside the same file? If they are in different file, how is the name of the files in the sites-enabled folder? With the other block it didn’t work because Nginx didn’t restart or because the block didn’t pick up the request?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.