st78
st78

Reputation: 8316

How to write redirect application in asp.net?

I need to move all requests from one domain to another. I want to change part of URL, like subdomain.olddomain/url -> subdomain.newdomain/url.

I was sure that this is piece of cake and wrote Application_Begin request as:

void Application_BeginRequest(object sender, EventArgs e)
    {
        string url = Request.Url.ToString().ToLower();
        string from = ConfigurationSettings.AppSettings["from"];
        if (url.IndexOf(from) >= 0)
        {
            url = url.Replace(from, ConfigurationSettings.AppSettings["to"]);
            Response.Redirect(url);
        }
        else
        {
            if (url.IndexOf("error.aspx") < 0)
            {
                Response.Redirect("Error.aspx?url=" + Server.UrlEncode(url));
            }
        }
    }

So far, I forget, that BeginRequest started only when file physically exist.

Any ideas, how I can make such redirect in asp.net without creating hundreds of old pages?

Upvotes: 0

Views: 725

Answers (4)

joe.liedtke
joe.liedtke

Reputation: 582

I would recommend using a tool like ISAPIRewrite [http://www.isapirewrite.com/] to manage this for IIS 6, or the built in URL Rewriting for IIS7.

No reason to reinvent the wheel...

Upvotes: 0

Dustin Laine
Dustin Laine

Reputation: 38543

I would recommend doing this on the DNS level. I would redirect with a permanent 301 redirect to ensure that your search engine rankings are not affected.

Upvotes: 0

Steven Sudit
Steven Sudit

Reputation: 19630

I believe you can specify an ASPX to run on 404 errors. That page can perform the redirect.

Upvotes: 0

D&#39;Arcy Rittich
D&#39;Arcy Rittich

Reputation: 171559

Not 100% sure, but I think if you uncheck the Check that file exists option in IIS, it should work. How you do this depends on the IIS version.

Upvotes: 1

Related Questions