Nginx没有将www重定向到www

So I a website which i cant reveal the url to at the moment. So i will refer to it as www.example.com for this question.

I want any attempt to get to my site to redirect to HTTPS:://www.example.com. However this is not the case. I have managed to get it working with redirecting http to https.

If i go to example.com it does redirect to www.example.com, but if i try any other route such as example.com/videos it just redirects to https://example.com/videos instead of https://www.example.com.

Here is my nginx config file:

server{

    listen 80;
    server_name example.com www.example.com;
    rewrite ^/(.*) https://www.example.com/$1 permanent;
}

server {
    listen 443 ssl;

    server_name example.com www.thevirtualpornwebsite.com;
    root /var/www/example_folder/public;


    index index.php index.html index.htm index.nginx-debian.html;


    ssl_certificate /home/example_user/example.com.chained.crt;
    ssl_certificate_key /home/example_user/example.com.key;

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';


    location / {
        rewrite ^/(.*)/$ /$1 permanent;
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    }
}

If anyone could help out that would be great!

If you check your HTTP server configuration you should see a rewrite rule

rewrite ^/(.*) https://www.example.com/$1 permanent;

This basically matches any given URL and this match is afterwards passed to the new location as the parameter $1 (check regular expressions).

If you remove this parameter, you should accomplish what you want:

rewrite ^/.* https://www.example.com/ permanent;