Written By Tycho de Waard (SU)
2020-03-04
As I got some people saying 'If I remember correcly, you did a blog once on redirects...where is it?' It still resides on the DNN Corp site but by popular demand, I repost here.
Redirects are important in many scenarios. Sometimes even crucial.
You could think of a marketeer that likes to communicatie www.domain.com/sale that is actually www.domain.com/travels-to-europe. This is the kind where you could create a page ‘sale’ in DNN and tell the settings to redirect www.domain.com/travels-to-europe.
But there are other scenarios where you have to dig into the web.config to accomplish what you want. This wiki is intended to start with some common scenarios, not to be complete at first start.
Be careful
Editing your web.config is something you need to do carefully. A mistake can take your website down, so always backup your web.config. I always have a web.config.old standby so I can use Filezilla to revert.
You can not put it anywhere
So, your web.config needs to be constructed in a certain way. There is the section of your redirect rules and where you put it in the existing web.config.
Your redirect section itself will look like
<rewrite>
<rules>
Here the actual rules
</rules>
</rewrite>
Now, where to put this bit...
Look for the section with httpProtocol.
<httpProtocol>
<customHeaders>
<remove name="X-Powered-By" />
</customHeaders>
</httpProtocol>
Your piece of work goes below this, so it will become like:
<httpProtocol>
<customHeaders>
<remove name="X-Powered-By" />
</customHeaders>
</httpProtocol>
<rewrite>
<rules>
Here the actual rules
</rules>
</rewrite>
Now, the different type of redirects. And all of them use regular expressions, so it helps if you can read them. If not, you can copy the code below and put in your own domain name.
Force https
These days, there is really no debating whether SSL is mandatory or not. Certificates are cheap or free and installing them has never been easier. Next stop is to ensure visitors are not getting the non-ssl version of your website. So, we’re going to redirect them to the SSL version.
Some hosting providers offer Plesk which has the option to force SSL but if you don’t have that option or feel more comfortable to use the web.config, here are 2 examples how you can do this.
Version 1
<rewrite>
<rules>
<rule name="Redirect to https" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions> <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
Version 2
<rule name="Redirect to https " enabled="true" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" negate="false" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTPS}" pattern="off" />
<add input="{HTTP_HOST}" pattern="www.mywebsite.com" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" />
</rule>
Second domain
Some customers have multiple domains that need to be directed to the main site. Most of the time, extra domains are bought for easier communication, claiming a generic word or alternations on the main domain name.
Of course, you don’t want to maintain multiple sites, not paying extra domain names in your certificates and not paying for modules that are licensed to a domain. So, again, we’re going to redirect. This time, the extra domain (domain 2) simply needs to be directed to domain 1
As the extra domain has not been indexed by Google, we can suffice with:
<rule name="redirect to another domain" enabled="true" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" negate="false" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_HOST}" pattern="www.domain2.com" /></conditions>
<action type="Redirect" url="http://www.domain1.com{REQUEST_URI}" redirectType="Permanent" />
</rule>
Catching old traffic
Worst nightmare: you win a new client, build a great webshop, far better than the old WooCommerce, smooth project, go live and...sales drop. Big time.
Good chance that 90% of the traffic is getting a 404 page and leave. The old shop was structured differently or even had pages ending at .php. Besides disappointed customers, Google will punish you as well. Page authority and domain authority are loosing points and are hard to regain.
Of course, the ideal situation is to redirect the old url of product A to the new url of product A. But this can easily result in thousands of redirects. If you’re not Aliexpress, you might consider this too much work. The alternative is to redirect everything ending on .php. For this scenario a redirect looks like:
<rule name="Redir old urls" stopProcessing="true">
<match url="(.*)" /><conditions logicalGrouping="MatchAny" trackAllCaptures="false">
<add input="{HTTP_HOST}{REQUEST_URI}" pattern="domainname.com/(.*)\.php" />
<add input="{HTTP_HOST}{REQUEST_URI}" pattern="www.domainname.com/(.*)\.php" /></conditions><action type="Redirect" url="/" redirectType="Permanent"/>
</rule>
What this basically says is: whatever the page or product is, when it ends on .php go to the homepage.
Tell me more, tell me more...
All written above is related to the redirects configured in IIS. There is a whole document about this on:
https://docs.microsoft.com/en-us/iis/extensions/url-rewrite-module/url-rewrite-module-configuration-reference
One more thing...
There was another addition by Mike Smeltzer on the original blog post which is the scenario of redirecting non-www to www.
The implementation of this has been explained on https://weblogs.asp.net/owscott/iis-url-rewrite-rewriting-non-www-to-www