views:

36

answers:

4

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?

+1  A: 

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.

RedFilter
This seems to be the way for me. On ASP.NET development server everything works just fine.
Sergey Osypchuk
The issue is that in IIS, check described in post http://forums.iis.net/t/1114839.aspx is unchecked :)So, locally it starts with BeginRequest, on live - with error.
Sergey Osypchuk
A: 

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

Steven Sudit
A: 

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.

Dustin Laine
I'm not sure I understand. A 301 redirect is HTTP. DNS might mean creating an alias to the previous hostname which points to the new server.
Steven Sudit
A: 

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...

joe.liedtke
Ok, but what are you rewriting from and to? It sounds like you would have to map all misses to a single redirect page. If so, I'm not sure that's an ideal usage of this tool.
Steven Sudit
"IIS URL Rewrite 2.0 enables Web administrators to create powerful rules" - This is great tool but it took sooooo long to find proper settings
Sergey Osypchuk