{"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

Sitewide Nginx 301 from non-www to www<\/h2>\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>\n

Or at<\/p>\n

\/etc\/nginx\/sites-available\/<site name><\/pre>\n

Where <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>\n

Make sure you replace “domain.com” with your actual domain, and that the rest of your configuration is in tact.<\/p>\n

There’s a great Stack Overflow thread on the topic of NGinx 301s here:\u00a0http:\/\/stackoverflow.com\/questions\/7947030\/nginx-no-www-to-www-and-www-to-no-www<\/a><\/p>\n

The WordPress Issue<\/h2>\n

Something that is very easily overlooked (well, in my experience anyway) is the sheer amount of stuff that WordPress does for you. This is great at times, but it can make debugging a nightmare. The WordPress installation is the last place I thought to look when dealing with what appeared to be a server-specific issue.<\/p>\n

The problem was that the “WordPress Address (URL)” and “Site Address (URL)” settings were both still pointing to the non-www version.<\/strong> Simply change these two settings to your desired version, press save, and you should be good to go.<\/p>\n

Note that if changing these two settings doesn’t work, then remember to check your wp_config.php file to make sure that these aren’t hardcoded into the installation. The above settings are saved to the database, whereas a hardcoded Site Address will take priority over the database call.<\/p>\n","protected":false},"excerpt":{"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…<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[7],"tags":[],"yoast_head":"\nRedirect non-www to www on Nginx server (+Wordpress) - JoeTannorella.com<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.joetannorella.com\/redirect-non-www-www-nginx-server-wordpress\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Redirect non-www to www on Nginx server (+Wordpress) - JoeTannorella.com\" \/>\n<meta property=\"og:description\" content=\"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...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.joetannorella.com\/redirect-non-www-www-nginx-server-wordpress\/\" \/>\n<meta property=\"og:site_name\" content=\"JoeTannorella.com\" \/>\n<meta property=\"article:published_time\" content=\"2014-09-06T14:49:49+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2014-09-06T14:59:03+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.joetannorella.com\/redirect-non-www-www-nginx-server-wordpress\/\",\"url\":\"https:\/\/www.joetannorella.com\/redirect-non-www-www-nginx-server-wordpress\/\",\"name\":\"Redirect non-www to www on Nginx server (+Wordpress) - JoeTannorella.com\",\"isPartOf\":{\"@id\":\"https:\/\/www.joetannorella.com\/#website\"},\"datePublished\":\"2014-09-06T14:49:49+00:00\",\"dateModified\":\"2014-09-06T14:59:03+00:00\",\"author\":{\"@id\":\"https:\/\/www.joetannorella.com\/#\/schema\/person\/84eafed4ad6cd6934b006e371c06c7ae\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.joetannorella.com\/redirect-non-www-www-nginx-server-wordpress\/\"]}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.joetannorella.com\/#website\",\"url\":\"https:\/\/www.joetannorella.com\/\",\"name\":\"JoeTannorella.com\",\"description\":\"Web Development | Digital Marketing | Business\/Startups | Analytics\/data\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.joetannorella.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.joetannorella.com\/#\/schema\/person\/84eafed4ad6cd6934b006e371c06c7ae\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.joetannorella.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/257001162d06da1a465f242dfa80dd7b?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/257001162d06da1a465f242dfa80dd7b?s=96&d=mm&r=g\",\"caption\":\"admin\"},\"url\":\"https:\/\/www.joetannorella.com\/author\/admin\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Redirect non-www to www on Nginx server (+Wordpress) - JoeTannorella.com","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.joetannorella.com\/redirect-non-www-www-nginx-server-wordpress\/","og_locale":"en_US","og_type":"article","og_title":"Redirect non-www to www on Nginx server (+Wordpress) - JoeTannorella.com","og_description":"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...","og_url":"https:\/\/www.joetannorella.com\/redirect-non-www-www-nginx-server-wordpress\/","og_site_name":"JoeTannorella.com","article_published_time":"2014-09-06T14:49:49+00:00","article_modified_time":"2014-09-06T14:59:03+00:00","author":"admin","twitter_misc":{"Written by":"admin","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.joetannorella.com\/redirect-non-www-www-nginx-server-wordpress\/","url":"https:\/\/www.joetannorella.com\/redirect-non-www-www-nginx-server-wordpress\/","name":"Redirect non-www to www on Nginx server (+Wordpress) - JoeTannorella.com","isPartOf":{"@id":"https:\/\/www.joetannorella.com\/#website"},"datePublished":"2014-09-06T14:49:49+00:00","dateModified":"2014-09-06T14:59:03+00:00","author":{"@id":"https:\/\/www.joetannorella.com\/#\/schema\/person\/84eafed4ad6cd6934b006e371c06c7ae"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.joetannorella.com\/redirect-non-www-www-nginx-server-wordpress\/"]}]},{"@type":"WebSite","@id":"https:\/\/www.joetannorella.com\/#website","url":"https:\/\/www.joetannorella.com\/","name":"JoeTannorella.com","description":"Web Development | Digital Marketing | Business\/Startups | Analytics\/data","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.joetannorella.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.joetannorella.com\/#\/schema\/person\/84eafed4ad6cd6934b006e371c06c7ae","name":"admin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.joetannorella.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/257001162d06da1a465f242dfa80dd7b?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/257001162d06da1a465f242dfa80dd7b?s=96&d=mm&r=g","caption":"admin"},"url":"https:\/\/www.joetannorella.com\/author\/admin\/"}]}},"_links":{"self":[{"href":"https:\/\/www.joetannorella.com\/wp-json\/wp\/v2\/posts\/333"}],"collection":[{"href":"https:\/\/www.joetannorella.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.joetannorella.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.joetannorella.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.joetannorella.com\/wp-json\/wp\/v2\/comments?post=333"}],"version-history":[{"count":4,"href":"https:\/\/www.joetannorella.com\/wp-json\/wp\/v2\/posts\/333\/revisions"}],"predecessor-version":[{"id":337,"href":"https:\/\/www.joetannorella.com\/wp-json\/wp\/v2\/posts\/333\/revisions\/337"}],"wp:attachment":[{"href":"https:\/\/www.joetannorella.com\/wp-json\/wp\/v2\/media?parent=333"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.joetannorella.com\/wp-json\/wp\/v2\/categories?post=333"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.joetannorella.com\/wp-json\/wp\/v2\/tags?post=333"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}