Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Jesper Ordrup 1019 posts 1528 karma points MVP
    Mar 18, 2011 @ 12:07
    Jesper Ordrup
    0

    Primary address redirect

    This code makes a permanent redirect from any other hostname to the specified hostname to the same page. I usually use to ensure that Google (and others) does not index the same content twice.

    In effect: 

    https://domain.dk:3837/mysubpage.aspx redirects to https://www.domain.dk/3837/mysubpage.aspx

        <%
                string primaryAddr "www.whatever.dk";
                var uri =  HttpContext.Current.Request.Url;
            
                if (uri.Host !primaryAddr || uri.Host.Contains(".local"))
                {
                    string newurl uri.Scheme @"://" primaryAddr;
      
                    if (uri.Port !80)
                        newurl +":" uri.Port;
      
                    newurl +uri.PathAndQuery;
                   HttpContext.Current.Response.Status "301 Moved Permanently";
                   HttpContext.Current.Response.AddHeader("Location",newurl);
      }
    %>

     

    Does this exsist in a package already or should I make it?

    /Jesper

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Mar 18, 2011 @ 13:17
    Tom Fulton
    0

    Hi Jesper,

    I typically achieve this using an IIS rewrite rule for "canonical domain name"

            <rewrite>
               
    <rules>
                   
    <rule name="CanonicalHostNameRule1">
                       
    <match url="(.*)" />
                       
    <conditions>
                           
    <add input="{HTTP_HOST}" pattern="^www\.mydomain\.com$" negate="true" />
                       
    </conditions>
                       
    <action type="Redirect" url="http://www.mydomain.com/{R:1}" />
                   
    </rule>
               
    </rules>
           
    </rewrite>

    Mike also posted an example on his blog of how to do this and some others also:  umbraco.miketaylor.eu/.../

    But actually, I just noticed that your code handles ports and SSL, which I'm not sure how that works in IIS.  So maybe it could be useful :)

    -Tom

Please Sign in or register to post replies

Write your reply to:

Draft