Setting up NGNIX to redirect from non-www to www

Posted February 23, 2016 3.3k views

Okay so the site was redirecting www to be non-www. But since there are quite a few backlinks that where www. Need to change the WordPress site so that it is www. Currently it is non-www. The issue was when changing the site config file for Ngnix.

server {
       listen 80;
       server_name localhost 999.999.99.99;
       root /var/www/;
       if ($http_host != "") {
                 rewrite ^$request_uri permanent;

Then restart Ngnix, that part works fine. But when updating the site URL and home URL to be both www in the WordPress options db table. I can not log into the dashboard site.

I know I will need to run a site update so that all links are www from non-www.

Any help would be appreciated.

edited by asb

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Submit an Answer
1 answer

Simplest solution is to create two “servers” one for non-www and one for www. In the non-www enter this config:

    listen       80;
    return       301$request_uri;

and on the www just your original config, but server_name with only

Just for info: return 301 [url] means that you permanently redirect client to the url.