Search code examples
.netiis-7http-status-code-301

IIS7 or .Net 301 Redirects from 1 domain to another


I have 2 domains. For the question, I will call them www.old.com and www.new.com. Both urls are pointing to the same IIS7 Site instance. I need to it up so that when someone goes to www.old.com they get a 301 redirect to www.new.com.

The tricky part is I am using URL rewrites for pages within the site. So www.old.com/About.aspx redirects to www.new.com/About. To get that to work with IIS7 URL rewrite rules, it also means that www.new.com/About.aspx redirects to www.new.com/About. That is fine and is not a big deal.

My issue is how do I redirect the main domain without losing the URL Rewrites from the sub pages?

I don't care if I use a module within IIS7 or if I need to do it in .NET code.


Solution

  • I think I would go for something like this using a rewrite module. Basically is saying that if is not going to your new domain then redirect it to it. passing the rest of the url that will by your custom rewrite module

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

    Hope it helps!