views:

146

answers:

4

Ok, I need to know how to do a redirect (where to put the code or specify the setting). We're redirecting from one app to another after we've moved the app.

So for example, if a user goes to existing.example.com/archive/

we want to redirect any requests that contain old.example.com to new.example.com. The rest in the url stays the same. So for example /archive/ is one example so we'd want to redirect them to the new location of this app which is new.example.com/archive/

I need to figure out how to check to see if the incoming URL to our existing site has existing.example.com, and if so replace that part only with the new new.example.com and keep the rest of what's in the url

I know you can do this in IIS 7 or programmatically. I guess I'm not understanding how to do this in either situation. I installed the IIS7 Rewrite plugin and ok fine, but here's what I don't get:

Pattern:

RedirectURL:

I don't see how in that interface I am able to match the existing.example.com and then what to put in the RedirectURL because I want to put the entire URL with only that existing.example.com changed to new.example.com in for the REdirectURL... and I don't see how I'd do this in IIS 7.

A: 

In your site's global.asax.cs file you can redirect from BeginRequest as follows. You can write the routine to replace the domain names as needed with regex or string.replace() or whatever you prefer.

protected void Application_BeginRequest(Object sender, EventArgs e){ 
    ...parse urls... 
    Response.Redirect(myNewPath)
}
jarrett
This will only work for requests processed by ASP.NET
John Sheehan
True John, depending on how things are setup they might have to change some handlers to make this work.
jarrett
-1. Sorry, Response.Redirect() does a 302 Found, not a 301 as requested in the OP.
RickNZ
Good Point on 302.
jarrett
A: 

For asp.net applications, I've had good experience with urlrewriting.net, a free component where you can configure redirects in your web.config. It allows you to enter regular expressions and specify the new URL with back references, e.g. something like this (untested):

<add name="newdomain" virtualUrl="^http\://old.example.com/(.*)$"
  rewriteUrlParameter="ExcludeFromClientQueryString"
  destinationUrl="http://new.example.com/$1"
  redirect="Domain"
  redirectMode="Permanent"
  ignoreCase="true" />

Drawback: This needs to be configured for every ASP.NET application (and you might need to reconfigure IIS to route everything through asp.net, or it might only work for .aspx files). If you want to redirect your complete domain, you are probably better off doing this on IIS level rather than on application level. For this, however, you might get better help on http://serverfault.com, which is where the sysadmins reside...

Heinzi
+3  A: 

Here's a post describing how to match one domain and redirect to another with everything else in tact using the IIS7 URL Rewrite add-in: http://weblogs.asp.net/owscott/archive/2009/11/27/iis-url-rewrite-rewriting-non-www-to-www.aspx

John Sheehan
thanks, this was what I was looking for as I don't know all the templated params yet for the IIS plugin.
CoffeeAddict
Feel free to accept the answer ;)
John Sheehan
+2  A: 

You don't need any complicated rewriting stuff to do such a trivial redirect, it's a basic web server feature. In IIS7 you can now choose not to install it (from World Wide Web Services->Common HTTP Features->HTTP Redirection), but it would be unusual to do so.

Edit the ‘Bindings’ of the main web site in IIS Manager so that it only responds to the ‘Host name:’ new.example.com, then create a new web site bound to host name old.example.com. For this site hit the ‘HTTP Redirect’ option and ‘Redirect requests to this destination:’ http://new.example.com/ with ‘Status code:’ 301.

In config XML terms:

<system.webServer>
    <httpRedirect enabled="true" destination="http://new.example.com/" httpResponseStatus="Permanent" />
</system.webServer>
bobince
so if an incoming url such as http://existing.com/archives came in it would redirect to http://new.example.com/archives leaving the rest of the url in tact after the first / ??
CoffeeAddict
Yes, as long as you don't tick the option that says “Redirect all requests to exact location”. There's a nice walkthrough here: http://www.trainsignaltraining.com/windows-server-2008-http-redirection/2008-03-27/
bobince