IIS Home @ it-notebook.org

Redirect specific files; Redirect from old domain to new domain

(Kristofer Gafvert, September 9, 2007)


Each week I take two questions from the IIS newsgroups (or IIS forums) that I think is of public interest, and I answer them here as well. Questions and answers may be modified to be more general and more useful for everyone.

This week I will answer two questions about redirecting.

Redirecting specific files only

I know I can redirect entire sites or virtual directories to another location. But what about certain individual files? A large site is being updated and I need all the old files to remain accessible. I want the site to continue serving pages from the local directory, which will contain all the new files and the old files, but it should redirect when a request is for an old file.

In IIS, you can configure redirects on file level. From within IIS Manager, this is done by right clicking the file, click Properties and then select "A redirection to a URL". This is however impractical if you need to do this on many files, so it can of course be done from a command line or by code. Using adsutil.vbs you can use the following command lines to set a redirection on an individual file:

CSCRIPT %SYSTEMDRIVE%\Inetpub\AdminScripts\adsutil.vbs CREATE W3SVC/1/ROOT/vdir/oldFile.htm IIsWebFile
CSCRIPT %SYSTEMDRIVE%\Inetpub\AdminScripts\adsutil.vbs SET W3SVC/1/ROOT/vdir/oldFile.htm/HttpRedirect "http://www.gafvert.info"

In the example above, 1 is the website ID, vdir a virtual directory and oldFile.htm the file in the virtual directory.

As you have probably already figured out, if you have many files that need to be redirected, there is a lot of work to do. If the old and new file can be distinguished by using a regex expression it would be far easier and wouldn't require so much editing. But unfortunately, the redirection feature in IIS does not support regex expressions. So a product like ISAPI Rewrite may be interesting to look at. A free ISAPI filter that should also be able to do redirects is IIRF (Ionic's ISAPI Rewrite Filter)

Redirect from the old domain name to the new domain name

We currently have two domain names for our website - the old domain name and the new domain name. We only want to use the new domain name and we want all requests sent to the old domain name to be redirected to the new domain name. This should be fairly easy to do using "A redirection to a URL" on the Home Directory tab of the old website. But we want the path to be included in the redirection, so if the user types www.olddomain.com/folder/file.htm, they should be redirected to www.newdomain.com/folder/file.htm. Or if they type www.olddomain.com/file.asp?id=56 we want them to be redirected to www.newdomain.com/file.asp?id=56. How do we accomplish this?

To do this, we need to use redirect variables. Without going into any details, I will first present the solution.

  • In IIS Manager, right click the website and click Properties.
  • Click on the Home Directory tab
  • Select "A redirection to a URL" and in the "Redirect to" textbox write: http://www.newdomain.com$V$Q
  • Check the boxes "The exact URL entered above" and "A permanent redirection for this resource"
Redirect, including path and query string

$V passes the requested URL without the server name. So if the original URL is www.olddomain.com/folder/file.asp?id=56, the $V variable holds /folder/file.asp. As you can see, this is not exactly what we want; we also want the query string. This is what $Q has; the query string including the question mark. We also need to select "The exact URL entered above" to prevent IIS from appending the original file name to the destination URL.

So why does this has to be so complicated? Should it not be fairly easy to append everything to the destination URL that is after the domain name? Well, since I don't have any insight in the IIS code, I can only guess. But for IIS (and any other web server) to know what file it should process, it must parse the URL. Everything that is on the right side of the question mark is not part of the path to the file; it is a parameter to the file (in this case an ASP file). So I can imagine that IIS parses the URL into several variables, one is the path, another is the query string.

Applies to [?]

IIS 6.0


Redirect Reference (IIS 6.0)
Redirecting Web Sites in IIS 6.0