0

On a Ubuntu VPS I am running Wordpress Multisite with nginx.

How can I redirect all sites-domains from non-www to www?

I want this to be done not site by site, but with a rule for every site after created .

ptheo
  • 103
  • 2

1 Answers1

2

Set up all your domains server blocks in Nginx with wwww. That is, only set up http://www.example.com etc type server blocks and then create a separate catch all server block to redirect all http://example.com etc type requests to http://www.example.com etc.

This is based on the configuration outlined here

http {
    [...]
    # Catch All for http://example.com domains
    # These will all redirect to http://www.example.com
    server {
        listen 80;
        return 301 http://www.$host$request_uri;
    }
    # Other server blocks (http://www.example.com etc)
    include /etc/nginx/conf.d/*.conf;
}

The way this will work is that all requests for http://example.com will always only be served by the catch all block which will bounce them to http://www.example.com.

Caveats are:

  1. Make sure you do not use the default_server directive anywhere.

  2. Requests for domains that resolve to your server but are not specifically defined, will return a redirect loop error to the user. To avoid this, if an issue, ensure that every domain that resolves to the server is defined.

Community
  • 1
  • 1
Dayo
  • 12,413
  • 5
  • 52
  • 67