IIS7 URL Rewrite multiple domains to a single domain including google analytics referral code

谁说我不能喝 提交于 2019-12-07 23:56:33

问题


I am trying to elegantly capture a large number of domains that need to redirect to a single domain for example:

mydomain.com, www.mydomain.com, mydomain-eu.com, mydomain.eu

all to the main domain:

www.mydomain.co.uk

This I can do fairly easily in IIS by creating a site - binding all the domains and doing a redirect.

However there is a catch. The client wants to track where those redirects came from in google analytics.

So they need some parameters passing in the redirect URL, example (using a domain from above):

mydomain.eu redirects to: www.mydomain.co.uk?utm_campaign=mydomain.eu&utm_source=mydomain.eu&utm_medium=referral

So - this is a good case to use a URL rewrite. This is what I have in the web.config:

   <rule name="Redirect" enabled="false" patternSyntax="Wildcard" stopProcessing="true">
            <match url="(.*)" />
            <conditions>
                <add input="{HTTP_HOST}" pattern="^www.mydomain.co.uk$" negate="true" />
            </conditions>
            <action type="Redirect" url="http://www.mydomain.co.uk/?utm_campaign={R:0}&amp;utm_source={R:0}&amp;utm_medium=referral" />
        </rule>

If the input does not match the domain I want to be I am redirecting it.

It works perfectly apart from one thing. I just cant get the parameter of the original domain. The {R:0} returns blank. I have tried {C:0} also. I have tested the patterns in the GUI and they tell me I should be getting the requested domain.

What am I doing wrong? Thanks.


回答1:


This is a guess but what about:

<rule name="Redirect" enabled="false" patternSyntax="Wildcard" stopProcessing="true">
        <match url="(.*)" />
        <conditions>
            <add input="{HTTP_HOST}" pattern="^www.mydomain.co.uk$" negate="true" />
        </conditions>
        <action type="Redirect" url="http://www.mydomain.co.uk/?utm_campaign={HTTP_HOST}&amp;utm_source={HTTP_HOST}&amp;utm_medium=referral" />
</rule>

Looked this up and it says you can use it this way in this documentation:

  • http://www.iis.net/learn/extensions/url-rewrite-module/url-rewrite-module-configuration-reference#UsingServerVars



回答2:


Thanks @rtpHarry for the guidance. Although the above rule didn't work for me, I managed to tweak it for my IIS7 server.

<rule name="Redirect" stopProcessing="true">
    <match url="(.*)" />
    <conditions>
        <add input="{HTTP_HOST}" pattern="^www.tsogosundigishare.com$" negate="true" />
    </conditions>
    <action type="Redirect" url="http://www.tsogosundigishare.com/" redirectType="SeeOther" />
</rule>

By removing the enabled="false" and patternSyntax from the rule, it converted the rule from being "Wildcard" to "Regular Expression" and adding redirectType="SeeOther" redirected properly.



来源:https://stackoverflow.com/questions/17787007/iis7-url-rewrite-multiple-domains-to-a-single-domain-including-google-analytics

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!