search

Home  >  Q&A  >  body text

Nginx multiple domain names point to the same website

My domain name is example.com
I want www.example.com to jump to example.com
My nginx configuration is

server {
       listen 80;
       server_name www.example.com example.com;
       root /var/www/web;

       if ($http_host == "www.example.com") {
                 rewrite ^ http://example.com$request_uri permanent;
       }

no. . .

PHP中文网PHP中文网2781 days ago598

reply all(3)I'll reply

  • 大家讲道理

    大家讲道理2017-05-16 17:31:53

    server {
      listen 80;
      server_name www.example.com;
      return 301 http://example.com/$request_uri;
    }
    
    server {
      listen 80;
      server_name example.com;
      root /var/www/web;
    }

    reply
    0
  • 某草草

    某草草2017-05-16 17:31:53

    if ($host = 'www.example.com' ) {
        rewrite ^/(.*)$ http://example.com/$1 permanent;
    }

    reply
    0
  • 天蓬老师

    天蓬老师2017-05-16 17:31:53

    if ($http_host = www.example.com) {
        rewrite  (.*)  http://example.com$1 permanent;
    }

    reply
    0
  • Cancelreply