Quantcast
Channel:
Viewing all articles
Browse latest Browse all 4

Nginx config for www to non-www and non-www to www redirection

0
0

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

I use the following code for my websites.

Redirect non-www to WWW

server {
        server_name example.com;
        return 301 http://www.example.com$request_uri;
}
<code> </code>

From WWW to non-WWW

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

In both cases, for other-www, I create a altogether different server { } block. IMHO, this is cleanest and optimised way to handle www to non-www and non-www to www redirection.

There are some WordPress plugins available there which can handle this at PHP-level. But for performance reason, always handle things in Nginx, that can be handled in Nginx alone! ;-)


Viewing all articles
Browse latest Browse all 4

Latest Images

Trending Articles





Latest Images