How to redirect subdomain to www domain using global.asax MVC4

I have a website under IIS7 with catch all subdomain as *.mydomain.com but some sites randomize subdomains like:

  • www2.mydomain.com
  • wwwmydomain.com
  • etc.mydomain.com
  • Any subdomain works perfectly loading my MVC page, but the google indexing doubles the url, because have two url differents.

    I would redirect permanently "301" any subdomain to www.mydomain.com using global.asax keeping the url and query string. eg

  • www2.mydomain.com/Controller/Action/ID?query=asb TO www.mydomain.com/Controller/Action/ID?query=asb

  • wwwmydomain.com/Controller/Action/ID?query=asb TO www.mydomain.com/Controller/Action/ID?query=asb

  • I want not to use IIS7 redirect because I will customize with C# the what subdomains should not be redirected.

    I don´t like to create page base for all controller.


    Assuming you are using IIS, you should be able to achieve this with ARR.

    Example:

    <rule name="SubDomain" stopProcessing="false">
                        <match url="(.*)" />
                        <conditions>
                            <add input="{HTTP_HOST}" pattern="^(?!www)(w+).mine.co.uk$" />
                        </conditions>
                        <action type="Rewrite" url="{C:1}/{R:1}" />
    </rule>  
    

    Reference: http://forums.iis.net/p/1155754/1895646.aspx

    链接地址: http://www.djcxy.com/p/70370.html

    上一篇: MVC 5子域

    下一篇: 如何使用global.asax MVC4将子域重定向到www域