Questions

How to redirect non-www to www url and vice-versa on nginx?

0

I want to redirect my non-www url to www url.



asked 1 year, 10 months ago
Reputation: 1





1 Answer

0

There are many ways to force Nginx to use either WWW version or non-WWW version of URLs for your site.

We use following codes all the time:

Redirect non-www to WWW

Single domain

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

All domains

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

From WWW to non-WWW

Single domain

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

All domains

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

answered 1 year, 10 months ago
Reputation: 1





Your Answer

Nothing to preview

Post Answer



Asked:  1 year, 10 months ago
Viewed:  445 times
Active:  1 year, 10 months ago