2

could u please tell me, how to make 301 permanent redirect in asp.net?

I have written code in Global.asax file but my web client says it is not working,

i have written follwing code in Global.asax file:

    protected void Application_BeginRequest(object sender, EventArgs e)
    {
        if (HttpContext.Current.Request.Url.ToString().ToLower().Contains(
"http://lsatfreedom.com"))
        {
            HttpContext.Current.Response.Status =
                "301 Moved Permanently";
            HttpContext.Current.Response.AddHeader("Location",
                Request.Url.ToString().ToLower().Replace(
                    "http://lsatfreedom.com",
                    "http://www.lsatfreedom.com"));
        } 

    }

Is it useful? Please help.

Thanks

Earlz
  • 62,085
  • 98
  • 303
  • 499
tina
  • 21
  • 1
  • 2
  • If you want the whole site to redirect why not a IIS redirect? – Shoban Jan 21 '11 at 04:40
  • and please tell me how to check whether it is working or not? – tina Jan 21 '11 at 04:51
  • If it is IIS7 then check this link : http://www.davelawlor.com/iis-7-setup-301-redirect – Shoban Jan 21 '11 at 04:52
  • How do you know that it is not working? What does "not working" mean? Are you getting errors or is the redirect just not happening? Can you post the response headers that are generated? – squillman Jan 21 '11 at 05:01

4 Answers4

2

First try to see if this redirection works in a page load. If yes, then try it with Begin_Request.

Hope this gives you some clue:

private void Page_Load(object sender, System.EventArgs e)
{
Response.Status = "301 Moved Permanently";
Response.AddHeader("Location","http://www.new-url.com");
}
Shoban
  • 22,920
  • 8
  • 63
  • 107
kheya
  • 7,546
  • 20
  • 77
  • 109
2

I think you are missing Response.Clear() and Response.End(), Please try with this.

For example:

protected void Application_BeginRequest(object sender, EventArgs e)
    {
        if (HttpContext.Current.Request.Url.ToString().ToLower().Contains(
"http://lsatfreedom.com"))
        {
           string sNewPage = Request.Url.ToString().ToLower().Replace(
                    "http://lsatfreedom.com",
                    "http://www.lsatfreedom.com");

            Response.Clear();
            Response.Status = "301 Moved Permanently";
            Response.AddHeader("Location", sNewPage);
            Response.End();
        } 
    }
Pankaj Agarwal
  • 11,191
  • 12
  • 43
  • 59
1

I believe you are missing a CompleteRequest()

So your code should look like this:

    if (HttpContext.Current.Request.Url.ToString().ToLower().Contains(
        "http://lsatfreedom.com"))
    {
        HttpContext.Current.Response.Status =
            "301 Moved Permanently";
        HttpContext.Current.Response.AddHeader("Location",
            Request.Url.ToString().ToLower().Replace(
                "http://lsatfreedom.com",
                "http://www.lsatfreedom.com"));
       CompleteRequest();
    } 

If you don't add the CompleteRequest, then ASP.Net will try to handle it itself, in which case the header may exist, but the Status may actually be overwritten between beginning the response and ending it. This would make it so that you don't get an actual redirect.

Earlz
  • 62,085
  • 98
  • 303
  • 499
1

I would change the web.config and add the following rule from this answer.

Forwarding http://mydomain.com/ctrlr/act/val to http://WWW.mydomain.com/ctrlr/act/val

That is how we are adding the www

Community
  • 1
  • 1
Jamie R Rytlewski
  • 1,172
  • 9
  • 22