HTTPeter
HTTPeter

Reputation: 247

nginx www redirect except other subdomains

Probably yet another nginx redirect question... couldn't find the answer, so:

How do I redirect http://domain.com to http://www.domain.com but do not rewrite any subdomain http://*.domain.com given the cloudfoundry nginx configuration which contains this server definition:

server {
    listen       80;
    server_name  _;
    server_name_in_redirect off;
}

I tried this configuration

server {
     server_name domain.com
     rewrite ^(.*) http://www.domain.com$1 permanent;
}

server {
    listen       80;
    server_name  _;
    server_name_in_redirect off;
}

but am getting infinite redirects.

Upvotes: 2

Views: 2692

Answers (2)

VBart
VBart

Reputation: 15110

server {
    listen 80;
    server_name domain.com
    return 301 http://www.domain.com$request_uri;
}

server {
    listen 80 default_server;
    ...
}

Upvotes: 1

kernel
kernel

Reputation: 154

Try replacing

server_name  _;

with

server_name *.domain.com;

Upvotes: 1

Related Questions