Redirect www to non-www and non-www to www?


#1

Hi!

I really like the EE! All wordpress sites work perfectly! But… I thought to transfer some other sites to the server, where I use EE and nginx won’t redirect www to non-www and vice versa.

Here is an example config where I want to redirect www to non-www

server {
    server_name www.domain.tld;
    return 301 $scheme://domain.tld$request_uri;
}
server {
        server_name domain.tld;
        listen   XXX.XXX.XXX.XXX:80;
        access_log /var/log/nginx/domain.tld.access.log rt_cache_redis; 
        error_log /var/log/nginx/domain.tld.error.log;
        root /var/www/domain.tld/;
        index index.php index.html index.htm;
    location / {
        try_files $uri $uri/ /index.php?$args;
    }
        error_page 404 /404.shtml;
        error_page 500 502 503 504 /404.shtml;
        location = /404.shtml {
              root /var/www/domain.tld/;
        }
    if (!-d $request_filename) {
            rewrite     ^/(.+)/$ /$1 permanent;
    }
        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_pass 127.0.0.1:9000;
                include fastcgi_params;
        }
    include common/locations.conf;
}

Where am I wrong? I have no idea why but still the www subdomain does not redirect to the domain itself. Any ideas how to achieve this? I have tried return(301 redirect), rewrite permanent(302 redirect)… Simply does not work… In wordpress this could be changed from wordpress backend but this cms does not have such an option… :smiley:


#2

I think I got it.

Has to be added on the first server {} part as well and it works… :wink:


#3