nGinx - SSL - Force www in url

I have Ubuntu 12.04 / nGinx running. Installed the SSL and all was working fine.

Now I want to force www to always be part of the URL so I currently have as the sites-available config:

force public http to http://www

server {

listen 80;

server_name example.com;

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

}

server {

listen 80;

server_name http://www.example.com;

[……]

}

force secure https to https://www

server {

listen 443 ssl;

server_name example.com;

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

}

server {

listen 443 ssl;

server_name https://www.example.com;

ssl_certificate /srv/ssl/www.example.com.crt;

sslcertificatekey /srv/ssl/www.example.com.key;

[……]

}

But now all i get is "The connection was interrupted page".

Error logs show nothing, nginx restart produces no errors or warnings.

The SSL used www in the name during the key / crt generation.

Any ideas? Suggestions would be great.

As always thanks!

Dave

1 Reply

Sorry, I dont use nGinx

Does it still use .htaccess?

If so just add this into the .htaccess file (altered accordingly)

Redirect non-www urls to www

RewriteEngine on

RewriteCond %{HTTP_HOST} !^www.yoursite.com

RewriteRule (.*) http://www.yoursite.com/$1 [R=301,L]

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct