How to redirect with “www” URL's to without “www” URL's or vice-versa?

后端 未结 8 2015
死守一世寂寞
死守一世寂寞 2020-12-08 05:15

I am using ASP.NET 2.0 C#. I want to redirect all request for my web app with \"www\" to without \"www\"

www.example.com to example.com

Or

example.co

相关标签:
8条回答
  • 2020-12-08 06:18

    We did this on IIS 6 quite simply. We essentially created a second virtual server that had nothing on it than a custom 404 page .aspx page. This page caught any requests for WHATEVERSERVER.com/whateverpage.aspx and redirected to the real server by changing the URL to be www.whateverserver.com/whateverpage.aspx.

    Pretty simple to setup, and has the advantage that it will work with any domains that come in to it (if you have multiple domains for instance) without having to setup additional rules for each one. So any requests for www.myoldserver.com/xxx will also get redirected to www.whateverserver.com/xxx

    In IIS 7, all this can be done with the URL writing component, but we prefer to keep the redirects off on this virtual server.

    0 讨论(0)
  • 2020-12-08 06:20

    There's a Stackoverflow blog post about this.

    http://blog.stackoverflow.com/2008/06/dropping-the-www-prefix/

    Quoting Jeff:

    Here’s the IIS7 rule to remove the WWW prefix from all incoming URLs. Cut and paste this XML fragment into your web.config file under

    <system.webServer> / <rewrite> / <rules>
    
    <rule name="Remove WWW prefix" >
    <match url="(.*)" ignoreCase="true" />
    <conditions>
    <add input="{HTTP_HOST}" pattern="^www\.domain\.com" />
    </conditions>
    <action type="Redirect" url="http://domain.com/{R:1}"
        redirectType="Permanent" />
    </rule>
    

    Or, if you prefer to use the www prefix, you can do that too:

    <rule name="Add WWW prefix" >
    <match url="(.*)" ignoreCase="true" />
    <conditions>
    <add input="{HTTP_HOST}" pattern="^domain\.com" />
    </conditions>
    <action type="Redirect" url="http://www.domain.com/{R:1}"
        redirectType="Permanent" />
    </rule>
    
    0 讨论(0)
提交回复
热议问题