0

Read and implemented everything but not able to redirect my domain www to non-www.

If I browse without www my site is opening fine.

My Config is as follows:

nginx version: nginx/1.10.3 (Ubuntu)

Distributor ID: Ubuntu
Description:    Ubuntu 16.04.5 LTS
Release:        16.04
Codename:       xenial

My current server-block config is as follows:

server {
    listen 80;
    server_name www.example.com;
    #return 301 $scheme://example.com$request_uri;
    #return 301 http://example.com$request_uri;
    rewrite ^(.*) http://example.com$1 permanent;
}

server {
        listen 80;
        listen [::]:80;

        server_name example.com;

        root /var/www/html/;
        index index.php index.html;
}
zarpio
  • 10,380
  • 8
  • 58
  • 71
  • Have you looked at https://stackoverflow.com/questions/7947030/nginx-no-www-to-www-and-www-to-no-www?rq=1 – Mtxz Sep 21 '18 at 21:12
  • Yes, I have looked at that. How can I debug what is going wrong? – zarpio Sep 21 '18 at 21:28
  • 1
    First of all, check if your www and non-www domains resolve to the same IP addresses. Also, you have different listening sockets in your www and non-www - IPv6 (`listen [::]:80;`) is only used for non-www one - this will cause issues if you are using IPv6. – Maxim Dounin Sep 21 '18 at 22:05
  • Can you please give me more details on it, what do you mean by, www and non-www domains resolve to the same IP addresses, how can I assure? – zarpio Sep 22 '18 at 00:11
  • I am using Amazon Cloud Server – zarpio Sep 22 '18 at 00:13
  • Ping www.example.com and example.com, do they resolve on the same IP ? – Mtxz Sep 22 '18 at 00:13
  • without www ping was hitting the server IP, but with www response is "Ping request could not find host www.example.com. Please check the name and try again." – zarpio Sep 22 '18 at 00:16

2 Answers2

1

try to add this server block:

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

restart Nginx:

sudo systemctl restart nginx

Note that if you are using HTTPS, the listen directive should be set to port 443 instead of 80

Use this curl command to ensure that the non-www domain redirects to the www domain (replace the highlighted part with your actual domain):

curl -I http://example.com

You should get a 301 Moved Permanently response, that shows the www redirect location, like this:

Sample Output:

HTTP/1.1 301 Moved Permanently
Server: nginx/1.4.6 (Ubuntu)
Date: Mon, 04 May 2015 18:20:19 GMT
Content-Type: text/html
Content-Length: 193
Connection: keep-alive
Location: http://www.example.com/
Nisarg
  • 1,631
  • 6
  • 19
  • 31
  • I have tried everything, please give me the steps how can I diagnose what is going wrong in my server block? plz note: I want to redirect from www to non-www because my non-www site is working fine. – zarpio Sep 21 '18 at 21:30
0

Modify first block

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

then sudo service nginx restart

Ahmed Abdelazim
  • 717
  • 7
  • 14