Remove "www" from domain by forwarding 301

Asked

Viewed 133 times

1

How do I redirect a www.dominio.com.br for dominio.com.br (www-free)?

I’m using Asp.net mvc.

  • This is usually in the hosting configuration of the website

  • I’m not sure, but the negative votes seem to me that were due to the way the question was written, I edited to be clearer. + 1 to the question

1 answer

1


Note: At first I thought it was a duplicate of How to redirect from non-www to www? but while reading I noticed that the question here is to remove "www" and not add, in the other question I did not see examples of removing "www" so I am answering here.

I found two answers in Soen:

  • Editing the web config. (the redirectType="Permanent" indicates permanent redirect which is 301):

    <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>
            </rules>
        </rewrite>
    </system.webServer>
    

    If you prefer to add www, switch to:

                <add input="{HTTP_HOST}" pattern="^domain\.com" /> 
            </conditions> 
            <action type="Redirect" url="http://www.domain.com/{R:1}" redirectType="Permanent" /> 
        </rule> 
    
  • Using the programming language, in this case an example with C#:

    protected void Application_BeginRequest(object sender, EventArgs e)
    {
       if (Request.Url.Host.StartsWith ("www") && !Request.Url.IsLoopback)
       {
          UriBuilder builder = new UriBuilder(Request.Url);
          builder.Host = Request.Url.Host.Replace("www.","");
          Response.StatusCode = 301;
          Response.AddHeader("Location", builder.ToString());
          Response.End();
       }
    }
    

    If you prefer to add www, switch to:

    UriBuilder builder = new UriBuilder(Request.Url);
    builder.Host = "www." + Request.Url.Host;
    
  • 1

    Thank you, @Guilherme Nascimento worked.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.