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

前端 未结 17 2179
予麋鹿
予麋鹿 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:08

    try this

        if ($host !~* ^www\.){
            rewrite ^(.*)$ https://www.yoursite.com$1;
        }
    

    Other way: Nginx no-www to www

    server {
      listen       80;
      server_name  yoursite.com;
      root /path/;
      index index.php;
      return       301 https://www.yoursite.com$request_uri;
    }
    

    and www to no-www

    server {
      listen       80;
      server_name  www.yoursite.com;
      root /path/;
      index index.php;
      return       301 https://yoursite.com$request_uri;
    }
    

提交回复
热议问题