{"id":333,"date":"2014-09-06T14:49:49","date_gmt":"2014-09-06T14:49:49","guid":{"rendered":"https:\/\/www.joetannorella.com\/?p=333"},"modified":"2014-09-06T14:59:03","modified_gmt":"2014-09-06T14:59:03","slug":"redirect-non-www-www-nginx-server-wordpress","status":"publish","type":"post","link":"https:\/\/www.joetannorella.com\/redirect-non-www-www-nginx-server-wordpress\/","title":{"rendered":"Redirect non-www to www on Nginx server (+WordPress)"},"content":{"rendered":"
Implementing a sitewide 301 isn’t particularly difficult. However, I kept hitting a redirect loop and just couldn’t figure out why. The problem was the URL as defined by WordPress was the non-www version, and not my desired www version. So, basically the server was redirecting from non-www to www, and then WordPress’ redirect was going from www to non-www, and thus the loop was formed.<\/p>\n
Navigate to your server block file. Depending on your method of setting up the server, this will either be at:<\/p>\n
\/etc\/nginx\/sites-available\/default<\/pre>\nOr at<\/p>\n
\/etc\/nginx\/sites-available\/<site name><\/pre>\nWhere <site name> is the name of the file containing your server block (best practice says that this should be your site name).<\/p>\n
We need to define a separate server block for the non-www version as well as the www version. For example:<\/p>\n
\r\nserver {\r\n server_name domain.com;\r\n rewrite ^(.*) http:\/\/www.domain.com$1 permanent;\r\n}\r\n\r\nserver {\r\n server_name www.domain.com;\r\n #The rest of your configuration goes here#\r\n}\r\n<\/pre>\nMake sure you replace “domain.com” with your actual domain, and that the rest of your configuration is in tact.<\/p>\n