Turn a 302 into a 301 RRS feed

  • Question

  • User598815691 posted

    I posted recently to get the Default.aspx page of a site redirecting to the root (basically so if anyone goes to www.domain.com/default.aspx they're get redirected to www.domain.com.

    I've managed it with the following code:

    <script runat="server">

        protected void Page_Load(object sender, EventArgs e)
            if ((Request.Url.AbsoluteUri.ToString () == "http://www.domain.com/default.aspx"))
                 Response.RedirectLocation = "http://www.domain.com";

     But how do I turn it from a 302 into a 301 redirect?

     Thanks for any help you can provide.

    Saturday, October 25, 2008 4:08 PM

All replies

  • User2130758966 posted

     You can put set the following:

    Response.StatusCode = 301;

    Sunday, October 26, 2008 6:57 AM
  • User1925464088 posted

    This is a known "gotcha" with ASP\ASP.NET as the Response.Redirect method returns a 302 HTTP code.

    You need to explicity set the an HTTP header to tell the browser that it's a 301 re-direct - search engines will pick this up.

    There are a number of ways of redirecting web pages for SEO using a 301 re-direct, but the right form for ASP.NET in c# is:

    Response.Status = "301 Moved Permanently";
    Response.AddHeader("Location", "your intended address");

    Tuesday, February 24, 2009 5:02 PM
  • User2130758966 posted

     Well, we both learned something today!


    Wednesday, February 25, 2009 5:26 PM
  • User373990221 posted

    I am having a similar problem. When designing a website locally i used the url including localhost for example "http://localhost:51079/Index/". I forgot to change them before getting the site hosted and now it keeps redirecting to the localhost even though i changed the code. To redirect i used:

     Response.Status = "301 Moved Permanently";

    Is there a way to change the location or any other solution to the problem?

    Thanks in advance

    Saturday, September 12, 2009 9:32 AM