简体   繁体   中英

Nginx - Wordpress multisite. Redirect non-www to www

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 .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM