URL rewrite in IIS7: make foo.com/bah/ redirect to example.com/foo/bah/
February 10, 2009 12:10 PM   Subscribe

Help me with a URL rewrite in IIS7

I want to implement a rewrite in IIS that would allow the following without having to create a new rule for each one.

www.foo.com/books/ redirects to www.example.com/foo/books/
www.foo.com/films/ redirects to www.example.com/foo/films/
www.foo.com/etc/ redirects to www.example.com/foo/etc/
www.foo.com/etc/etc/ redirects to www.example.com/foo/etc/etc/

I am familiar with the steps in using url rewrite module in IIS7 if that makes passing on instructions a bit easier.
posted by furtive to Computers & Internet (3 answers total)
 
Try importing this into IIS with the URL Rewrite module:

RewriteCond %{HTTP_HOST} ^www.foo.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

posted by Blazecock Pileon at 12:47 PM on February 10, 2009


I'm not familiar with the rewrite module in IIS 7, but if BP's solution doesn't work, you could accomplish this (at least in IIS 6) by setting the custom error page for 404 errors for the original IIS site to something like '/NewSiteRedirect.asp', and then create that NewSiteRedirect.asp page at the root of the original site, with the following content:

<%@ Language=VBScript %>
<%
Option Explicit

Const NewServerName = "newservername.com"

Dim queryStringArray, strOriginalUrl, objRegExp, strNewUrl

' QueryString is going to come in as 404;<old URL value>.
queryStringArray = Split(Request.QueryString, ";")
strOriginalUrl = queryStringArray(1)

Set objRegExp = new RegExp
objRegExp.Pattern = "^http://[^/]+(/.*)$"
strNewUrl = objRegExp.Replace(strOriginalUrl, "http://" & NewServerName & "$1")

Response.Redirect strNewUrl
%>

Anyway, that's kind of a long way around, and there are probably better approaches. But that should work, if nothing else does. Good luck!
posted by Brak at 1:14 PM on February 10, 2009


Best answer: In the end I used the information available here: Creating rewrite rules for the URL Rewrite Module
posted by furtive at 10:28 PM on July 7, 2009


« Older Testing-the-Waters-Filter   |   What emulators run best on a PSP? Newer »
This thread is closed to new comments.