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

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

    If you don't want to hardcode the domain name, you can use this redirect block. The domain without the leading www is saved as variable $domain which can be reused in the redirect statement.

    server {
        ...
        # Redirect www to non-www
        if ( $host ~ ^www\.(?.+) ) {
           rewrite ^/(.*)$ $scheme://$domain/$1;
        }
    }
    

    REF: Redirecting a subdomain with a regular expression in nginx

提交回复
热议问题