tina
tina

Reputation: 21

how to make 301 permanent redirect in asp.net

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

Upvotes: 2

Views: 3704

Answers (4)

Pankaj Agarwal
Pankaj Agarwal

Reputation: 11309

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();
        } 
    }

Upvotes: 2

Jamie R Rytlewski
Jamie R Rytlewski

Reputation: 1221

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

Upvotes: 1

kheya
kheya

Reputation: 7631

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");
}

Upvotes: 2

Earlz
Earlz

Reputation: 63905

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.

Upvotes: 1

Related Questions