Nginx no-www to www and www to no-www

前端 未结 17 2415
予麋鹿
予麋鹿 2020-11-22 09:53

I am using nginx on Rackspace cloud following a tutorial and having searched the net and so far can\'t get this sorted.

I want www.mysite.com to go to mysite.com as

17条回答
  •  庸人自扰
    2020-11-22 10:10

    Redirect non-www to www

    For Single Domain :

    server {
            server_name example.com;
            return 301 $scheme://www.example.com$request_uri;
    }
    

    For All Domains :

    server {
            server_name "~^(?!www\.).*" ;
            return 301 $scheme://www.$host$request_uri;
    }
    

    Redirect www to non-www For Single Domain:

    server {
            server_name www.example.com;
            return 301 $scheme://example.com$request_uri;
    }
    

    For All Domains :

    server {
             server_name "~^www\.(.*)$" ;
             return 301 $scheme://$1$request_uri ;
    }
    

提交回复
热议问题