6

Recently I migrated an ASP.net site to ASP.net MVC site. Earlier there were two host headers one mydomain.com and another is www.mydomain.com. My SEO says you should use only one url "www.domain.com" for SEO advantage.

I am looking for an option to do 301 permanent redirect all mydomain.com request to www.mydomain.com.

The site is hosted in IIS6 and developed in ASP.net MVC 4.

Karthick
  • 443
  • 4
  • 10

4 Answers4

19

You can do this from your web.config file

<system.webServer>
    <rewrite>
        <rules>
          <rule name="Redirect to WWW" stopProcessing="true">
            <match url=".*" />
            <conditions>
              <add input="{HTTP_HOST}" pattern="^example.com$" />
            </conditions>
            <action type="Redirect" url="http://www.example.com/{R:0}"
                 redirectType="Permanent" />
          </rule>
        </rules>
    </rewrite>
</system.webServer>
Michal Hosala
  • 5,570
  • 1
  • 22
  • 49
Tommy
  • 39,592
  • 10
  • 90
  • 121
  • This is a nice answer, @Tommy; not a line of c# in sight. – Moby's Stunt Double Apr 11 '13 at 15:43
  • You must be using IIS7 or greater in order to use the URL Rewrite module. This is the way to go if you have at least IIS7, but it will not work for IIS6. – Craig Tullis Apr 13 '13 at 22:35
  • @Tommy - would it be better to use the IIS7 URL Rewrite module or the .Net routing functionality within the webApp iself to do a redirection if you have the ability to use both? Or (third option) should I just respond to both and maintain URL for both using a reverse proxy in the URL Rewrite module? ...although that last 3rd option is not ideal for SEO I would think. – johntrepreneur Jul 22 '13 at 21:41
  • 2
    @johntrepreneur - this is using the IIS URL Rewrite module, just from the configuration file of the app. If you have IIS 7 and can use this method, then either going through the IIS manager console or using the web.config should function the same way. If you use the .NET routing functionality, you are having to pass the request into the .NET pipeline which may cause a slight performance degradation. I would use the .NET routing only in the case of not being able to use the rewrite module IMHO. – Tommy Jul 23 '13 at 01:21
  • If I may, what is the `httpRedirect` element good for in here? – Michal Hosala Nov 19 '15 at 23:46
  • @MichalHosala - I am 99% sure it does nothing. I pulled this from one of our production config files that we do this, appears we left some other attempts in the file as well (notice the enabled = false). But, great catch, its been on SO for over 2 years and no one has asked about it. – Tommy Nov 20 '15 at 01:07
  • @MichalHosala - and why don't you like mydomain.com? :P – Tommy Nov 20 '15 at 01:14
  • 1
    @Tommy believe it or not, some policy had to be introduced in the last 2 years because I received an error on saving my changes that I cannot use mydomain.com within the answer anymore :D See [here](http://meta.stackexchange.com/q/208963/299595). – Michal Hosala Nov 20 '15 at 01:15
  • 1
    @MichalHosala - And its a well supported by rationale policy too. +1 for digging that up, I had no idea. Cool! – Tommy Nov 20 '15 at 02:03
5

You could use config or Url Rewriter in IIS, but the best method I've found is just to put some code in Application_BeginRequest() in your global.asax.cs like this:

var HOST = "www.mydomain.com";

if ( !Request.ServerVariables[ "HTTP_HOST" ].Equals(
  HOST,
  StringComparison.InvariantCultureIgnoreCase )
)
{
  Response.RedirectPermanent(
    ( HttpContext.Current.Request.IsSecureConnection ? "https://" : "http://" )
    + HOST
    + HttpContext.Current.Request.RawUrl );
}

Because you're doing this in code, you can have whatever logic you need on a per-request basis.

Nick Butler
  • 24,045
  • 4
  • 49
  • 70
  • i was tried rule in config in all aspect but was not succeed in IIS 7. but this solution works from me. Thanks a lot. – CrazyDev Dec 20 '16 at 07:45
0

Unfortunately, the URL rewrite module does not work with IIS6 (only IIS7 or greater). Have you considered creating your own HttpModule, something like this this?

IIS 6 how to redirect from http://example.com/* to http://www.example.com/*

Or you could potentially use a 3rd party solution like one of these:

http://iirf.codeplex.com/

http://www.urlrewriting.net/149/en/home.html

http://www.isapirewrite.com/

http://urlrewriter.net/

Community
  • 1
  • 1
Craig Tullis
  • 9,939
  • 2
  • 21
  • 21
0

(IIS 7 or greater required)

from http://www.codeproject.com/Articles/87759/Redirecting-to-WWW-on-ASP-NET-and-IIS

(Similar to above solution, but not requiring you to add your own domain name.)

<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <clear />
                <rule name="WWW Rewrite" enabled="true">
                    <match url="(.*)" />
                    <conditions>
                        <add input="{HTTP_HOST}" negate="true"
                            pattern="^www\.([.a-zA-Z0-9]+)$" />
                    </conditions>
                    <action type="Redirect" url="http://www.{HTTP_HOST}/{R:0}"
                        appendQueryString="true" redirectType="Permanent" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

Note that you will most likely see squiggly lines under the tag with a message that the tag is invalid. I got this message but, in fact, it worked just fine.

If you want the intellisense to work, you can try this update here...

http://ruslany.net/2009/08/visual-studio-xml-intellisense-for-url-rewrite-1-1/

More information about httpRedirect can be found here...

http://www.iis.net/configreference/system.webserver/httpredirect

Carter Medlin
  • 11,857
  • 5
  • 62
  • 68