It took me a while to brush up on regexes, but it was definitely worth it. The thing is, at the beginning I had only 1 domain – sunamo.cz. Back then it was bought for me by someone else, who later cut off contact with me, so I let the domain expire and then bought it myself.
In the meantime, I also bought sunamo.cz. At ASPOne they told me that a domain alias would be best for me – meaning the browser shows www.sunamo.cz, but the content from www.sunamo.cz is displayed. The only thing I had to duplicate was user cookies, and 4 times at that – for sunamo.cz, sunamo.cz, www.sunamo.cz and www.sunamo.cz.
But then Google Analytics started reporting that it was receiving visits from a different domain than the one entered when the account was created. Fine, I said to myself, I’ll drop one domain and redirect it. But which one – at first it looked like sunamo.cz, mainly because the URL shortener shortened addresses to the sunamo.cz domain.
But then I told myself I would be creating unnecessary problems for myself and for ASPOne if I had to rename all references to sunamo.cz to .net, and that I would try to do it with URL Rewrite instead and redirect all requests from sunamo.cz to sunamo.cz. In the end, I found this working solution:
<system.webServer>
<rewrite>
<rules>
<clear></clear>
<rule name="Redirect to www" enabled="true" stopProcessing="true">
<match url="(.*)"></match>
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^sunamo.cz$"></add>
</conditions>
<action type="Redirect" url="https://sunamo.cz/{R:1}" redirectType="Permanent"></action>
</rule>
<rule name="Rewrite rule1 for net" stopProcessing="true">
<match url="((.*)?)$" negate="false"></match>
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="(.*.net)$"></add>
</conditions>
<action type="Redirect" url="https://sunamo.cz/{R:0}" appendQueryString="true"></action>
</rule>
<rule name="Redirect from www net" enabled="true" stopProcessing="true">
<match url="(.*)$"></match>
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{URL}" pattern="http://www(.*)net/?q=(.*)"></add>
</conditions>
<action type="Redirect" url="http://www.sunamo.cz/?q={C:2}" appendQueryString="true" redirectType="Permanent"></action>
</rule>
</rules>
</rewrite>
</system.webServer>The first rule redirects pages of the sunamo.cz domain from the non-www version to the www version. The second rule then redirects all requests for the .net domain to www.sunamo.cz. I handled this part without much trouble; the last one is a redirect that sends requests for the home page, if it contains the q parameter, to sunamo.cz.
Now it has occurred to me that this is unnecessary – it would be enough to remove the condition that I didn’t even know I had there, whose purpose was to allow shortened links to be opened only from sunamo.cz. But now I don’t have to. And the problem with Google Analytics, the duplicate Google index and many other things is solved – whether it is ashx, html, aspx or anything else on sunamo.cz, it always gets redirected before the page even starts loading/executing.