Another post in my Umbraco Basics series. This post will explain how to easily redirect domain.com to www.domain.com
When you set up your domain you usually set both a www version and a non-www version of your URL pointing at your new delightful Umbraco site. This means that domain.com and www.domain.com will both take the user to your site and on the face of it, it will work great.
But then you add your site to the Google Search Console (which used to be called Google Webmaster Tools) and suddenly you get complaints about duplicate content. Upon further investigation, you find that Google is seeing domain.com/about and www.domain.com/about as different pages and that's a problem as it is finding two pages on your domain with the same content... It doesn't feel like a problem, but Google is complaining so you HAVE to do something about it.
Yes, there are things you can do with the canonical tag in the headers but surely the best solution is to simply permanently redirect all traffic from non-www traffic to the proper URL with a www prefix.
In some cases, you may want it the other way, especially when the domain doesn't look right with a www in the URL. For example, I worked at a company called Twelve Oaks Software and the domain is https://twelveoaks.software which was deemed by us to look better than https://www.twelveoaks.software (By the way, I built that site in Umbraco, take a look). We fix this by doing the redirect the other way around, but most people want www at the beginning of their domain.
So how do you do it? Well, the answer is relatively simple as long as you have access to the code base and in particular the web.config file.
The web.config file is usually within the files created from a publish so take a look in the publish/compiled folder or on the web server root folder. Before you modify this file, please make sure that you make a backup of it.
The web.config file is a simple text file in xml format, to enter a rule for the permanent redirect you create a <rule> under <system.webServer> <rewrite> <rules>
The rule takes the form of a <match> and an <action>. It requires a RULENAME as shown below and if you have more than one rule in the file, then you must give them separate names or you will get an error.
Then the rule in the web.config file would look like this:
<rule name="ensurewww" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{CACHE_URL}" pattern="^(.+)://(?!www)(.*)" />
</conditions>
<action type="Redirect" url="{C:1}://www.{C:2}" redirectType="Permanent" />
</rule>
For it to work within the web.config file, it needs to be in the correct place. There may already be a <system.webServer>, <rewrite> and <rules> sections, in which case add the rule code above just under the <rules> category as below.
If the above categories don't exist as tags in the web.config file then you will need to add them so it looks something like this. You can add multiple <rule> configurations to the <rules> section.
<system.webServer>
<rewrite>
<rules>
<rule name="ensurewww" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{CACHE_URL}" pattern="^(.+)://(?!www)(.*)" />
</conditions>
<action type="Redirect" url="{C:1}://www.{C:2}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
Once you save the web.config overwriting the old one, the site should automatically see the difference in the configuration and recompile the site to run.
Once in place, typing domain.com into your browser will automatically be redirected to www.domain.com! Simple.
If for some reason the redirection is not working, it could mean that your hosting company is actively stopping you making redirects or they have not configured the web server to perform redirects. If this is the case then you will need to contact your hosting company directly and ask them to configure the server for redirects.
Now you shouldn't get any complaining from Google, all traffic will work under your www domain and everything is good in the world again!
Hey, I really could do with your help! If you find this article interesting, could you please do me a favour by either sharing it on your site or on social media. I would love to hear yours and other peoples' thoughts on this subject. And if this or any other content on the site has helped you and you would like to show your appreciation, then you can always
buy me a coffee ☕️ It would make the time I put into this more than worthwhile! Thank you 😃