Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I am trying to get the following to work so that no matter what you type in the URL bar you always go to "https://www.". So far www. does not go to https://www. and https:// does not go to https://www.

Below is my code. What needs to be done?

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

    server {
        listen       80;
        server_name  www.website.com;
        #rewrite ^(.*) https://www.website.com$1 permanent;
        root /home/knownsrv/public_html;

        listen       443 ssl;
        #server_name  website.com www.website.com;
        #root /home/knownsrv/public_html;
        ssl_certificate      /root/knownsrv.crt;
        ssl_certificate_key  /root/knownsrv.key;
        #ssl_client_certificate /root/chain.cer;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   /home/knownsrv/public_html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
location ~ \.php$ {
            root           /home/knownsrv/public_html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
}
}
share|improve this question

1 Answer 1

In the server block add:

if ($host !~* ^www\.) {
        rewrite ^(.*)$ https://www.website.com$1 permanent;
}

So your config should look something like:

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

server {
    listen 443 ssl;
    server_name website.com www.website.com;
    root /home/knownsrv/public_html;

    if ($host !~* ^www\.) {
        rewrite ^(.*)$ https://www.website.com$1 permanent;
    }

    ssl_certificate /root/knownsrv.crt;
    ssl_certificate_key /root/knownsrv.key;

    location / {
        root /home/knownsrv/public_html;
        index index.html index.htm;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }

    location ~ \.php$ {
        root            /home/knownsrv/public_html;
        fastcgi_pass    127.0.0.1:9000;
        fastcgi_index   index.php;
        fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include         fastcgi_params;
    }
}
share|improve this answer
    
Can't seem to get the non ssl to redirect to SSL –  K Mack May 18 '13 at 4:37
    
I edited the config just to make it more clear. –  chr1x2 May 18 '13 at 6:06
    
I've just tried it and it hasn't made any difference –  jackweirdy May 24 '13 at 17:16

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.