0

It seems, that I'm unable to redirect my webpage from WWW to non-WWW variant. At first I tried to do this by using IIS rewrite module like this:

<rewrite>
      <rules>
        <rule name="Canonical" stopProcessing="true">
          <match url=".*" />
          <conditions>
            <add input="{HTTP_HOST}" pattern="^www[.](.+)" />
          </conditions>
          <action type="Redirect" url="http://{C:1}/{R:0}" redirectType="Permanent" />
        </rule>
      </rules>
 </rewrite>

But it keeps me redirecting from www.xxx.yyy.com to www.yyy.com/xxx which is not i want to do.

Next solution I've tried, was to do it in Global.asax file in Application_BeginRequest methods like this:

protected void Application_BeginRequest(object sender, EventArgs ev)
{
    if (Request.Url.Host.StartsWith("www", StringComparison.InvariantCultureIgnoreCase))
    {
        Response.Clear();
        Response.AddHeader("Location", 
            String.Format("{0}://{1}{2}", Request.Url.Scheme, Request.Url.Host.Substring(4), Request.Url.PathAndQuery)
            );
        Response.StatusCode = 301;
        Response.End();
    }
}

but this time it keeps saying that there is a redirect loop. Thanks for help in advance :)

Slammerek
  • 66
  • 1
  • 2
  • 9

1 Answers1

-1

Well, i sugest this code, its very easy and short ;)

protected void Application_BeginRequest(object sender, EventArgs ev)
{
    string address = Request.Url.AbsoluteUri;
    if (address.Contains("www"))
    {
        Response.Redirect("site.com");
    }
}
  • redirects like this should not happen on application level if possible – Nikola Sivkov Feb 03 '14 at 20:00
  • Your code works after deletion of my cookies, but i still get wrong redirection. It doesn't matter to which site i redirect it to because it'll always use this: **www.xxx.yyy.com** to **www.yyy.com/xxx**. Is it possible that owner of **yyy.com** domain is somehow rewriting everything i do? – Slammerek Feb 03 '14 at 20:10
  • btw: on localhost it works perfectly. If i use "localhost" instead of "www" i get redirected properly. – Slammerek Feb 03 '14 at 20:13
  • @Slammerek if when you enter url and this url its a subdomain, dont have any reason to show de site using /xxx, sure the subdomain need be in another fold but normally dont have this problem. Now i'm curious, your site redirect to url that you dont have control? And just to have sure, enter the url with browser and check the redirect of the "www.xxx.yyy.com" – Dietrich Prg Feb 04 '14 at 10:14