{"id":206,"date":"2011-01-24T19:11:42","date_gmt":"2011-01-25T00:11:42","guid":{"rendered":"http:\/\/www.compdigitec.com\/labs\/?p=206"},"modified":"2011-01-24T19:11:49","modified_gmt":"2011-01-25T00:11:49","slug":"redirect-non-www-domain-to-www-in-apache","status":"publish","type":"post","link":"https:\/\/www.compdigitec.com\/labs\/2011\/01\/24\/redirect-non-www-domain-to-www-in-apache\/","title":{"rendered":"Redirect non-www domain to www in Apache"},"content":{"rendered":"<p>Search engines will often consider the non-www and www sites of a domain as separate websites (example.com vs <strong>www<\/strong>.example.com). In order to solve this, you will often want to redirect the non-www part of the website (example.com) to the www part (www.example.com). So this way if your visitor types example.com or www.example.com they will wind up at your home page at www.example.com.<\/p>\n<p>First have your site configuration file (with the VirtualHost) opened in your favourite text editor, then make sure the ServerName of the VirtualHost is set to the www version:<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;VirtualHost *:80&gt;\r\n\tServerAdmin you@example.com\r\n\tServerName www.example.com\r\n\r\n\t&lt;!-- other VirtualHost configs here... --&gt;\r\n&lt;\/VirtualHost&gt;\r\n<\/pre>\n<p>Then add another VirtualHost at the bottom of that (in the same file), replacing your variables:<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;VirtualHost *:80&gt;\r\n\tServerName example.com\r\n\tRedirect permanent \/ http:\/\/www.example.com\/\r\n&lt;\/VirtualHost&gt;\r\n<\/pre>\n<p>This should look something like this in the end (all in one file):<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;VirtualHost *:80&gt;\r\n\tServerAdmin you@example.com\r\n\tServerName www.example.com\r\n\r\n\t&lt;!-- other VirtualHost configs here... --&gt;\r\n&lt;\/VirtualHost&gt;\r\n\r\n&lt;VirtualHost *:80&gt;\r\n\tServerName example.com\r\n\tRedirect permanent \/ http:\/\/www.example.com\/\r\n&lt;\/VirtualHost&gt;\r\n<\/pre>\n<p>When you are done save the file, close it and reload Apache:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">sudo \/etc\/init.d\/apache2 reload<\/pre>\n<p>This is a very safe and effective way to enforce your SEO and this solution does not require the rewrite engine either, which makes it very fast and safe. It performs the 301 redirect in the search-engine-approved manner and it will make sure your search engine popularity is not being cut in half by the www and non-www.<\/p>","protected":false},"excerpt":{"rendered":"<p>Search engines will often consider the non-www and www sites of a domain as separate websites (example.com vs www.example.com). In order to solve this, you will often want to redirect the non-www part of the website (example.com) to the www part (www.example.com). So this way if your visitor types example.com or www.example.com they will wind [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[],"_links":{"self":[{"href":"https:\/\/www.compdigitec.com\/labs\/wp-json\/wp\/v2\/posts\/206"}],"collection":[{"href":"https:\/\/www.compdigitec.com\/labs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.compdigitec.com\/labs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.compdigitec.com\/labs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.compdigitec.com\/labs\/wp-json\/wp\/v2\/comments?post=206"}],"version-history":[{"count":0,"href":"https:\/\/www.compdigitec.com\/labs\/wp-json\/wp\/v2\/posts\/206\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.compdigitec.com\/labs\/wp-json\/wp\/v2\/media?parent=206"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.compdigitec.com\/labs\/wp-json\/wp\/v2\/categories?post=206"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.compdigitec.com\/labs\/wp-json\/wp\/v2\/tags?post=206"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}