简体   繁体   English

Nginx-WordPress多站点。 将非www重定向到www

[英]Nginx - Wordpress multisite. Redirect non-www to www

On a Ubuntu VPS I am running Wordpress Multisite with nginx. 在Ubuntu VPS上,我正在使用Nginx运行Wordpress Multisite。

How can I redirect all sites-domains from non-www to www? 如何将所有站点域从非www重定向到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 . 使用wwww在Nginx中设置所有域服务器块。 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. 也就是说,仅设置http://www.example.com等类型的服务器块,然后创建单独的catch all服务器块以将所有http://example.com等类型的请求重定向到http://www.example.com

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 . 这种工作方式是,对http://example.com的所有请求将始终仅由catch all块处理,该块会将其反弹到http://www.example.com

Caveats are: 注意事项是:

  1. Make sure you do not use the default_server directive anywhere. 确保您不在任何地方使用default_server指令。

  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. 为了避免这种情况(如果出现问题),请确保定义了解析到服务器的每个域。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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