Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have a folder on my server like this one:

C:\inetpub\wwwroot\blah

In IIS, I also have a "/blah" virtual folder set up, pointing to C:\BLAH

Naturally, when site visitors type in http://www.myserver.com/blah/test.html, IIS looks for C:\BLAH\test.html, not C:\inetpub\wwwroot\blah\test.html. For the most part, this is the behavior I want, because most of the files are in C:\BLAH. However, when the file DOES exist in C:\inetpub\wwwroot\blah, I'd like the server to look for it there first, and then look in C:\BLAH if it doesn't find it. Basically I'd like the files in C:\inetpub\wwwroot\blah to be an "exception" to the virtual directory. Have the server return the files from that folder, if they exist, but if they don't pull them from C:\BLAH instead of returning a 404.

I am running IIS 5.0 on Windows Server 2000.

Is this possible? Thanks.

share|improve this question
    
Which version of IIS? –  Sam Jun 9 '09 at 16:39
    
It's Windows 2000 Server, so... IIS 5.0, I'd imagine. –  Joshua Carmody Jun 9 '09 at 18:04

3 Answers 3

up vote 1 down vote accepted

This is kind of ghetto, but it might be a simple way to solve your problem.

  • Create a second virtual directory that points to c:\blah.
  • In the "blah" virtual directory (which points to C:\inetpub\wwwroot\blah), setup a custom error handler for 404 errors that is an ASP or ASP.Net page in c:\inetpub\wwwroot\blah\
  • In the 404 handler ASP.Net page,
    • pull the requested URL (it should be the only thing in Request.QueryString)
    • chop off everything but the filename requested
    • redirect to "/cblah/FILENAME".

Here is a little snippet of ASP.Net code that could act as the 404 handler:

string qs = Page.Request.QueryString.ToString();
qs = Server.UrlDecode(qs);
int c = qs.LastIndexOf("/");
string filepath = qs.Substring(c+1);
Response.Redirect("/cblah/"+filepath);
share|improve this answer
    
Brilliant! I'm sure that would work. –  Joshua Carmody Jun 11 '09 at 19:19

You could probably do this through a custom written ISAPI filter, but I am not aware of any way within IIS or an existing product that would do exactly what you're looking for.

share|improve this answer

IIRF is a free URL Rewriter which has a capability to check for the existence of a directory, before rewriting. The rules would look like:

# This rule matches, eg, http://server/blah/test.html
# It does "no rewrite" (-) if the file c:\blah\test.html does NOT exist. 
# The url stub /blah must map to an existing vdir. 
RewriteCond  c:\blah\$1        !-f
RewriteRule  ^/blah/(.*)$      -  [L]

# If the file does exist, rewrite to the alternative vdir. 
RewriteCond  c:\blah\$1           -f
RewriteRule  ^/blah/(.*)$         /override-vdir/$2    [L]

You can also wildcard "blah". To do that you'd need a convention for naming the override vdirs. It would look something like this:

# This rule matches, eg, http://server/blah/test.html
# It does nothing if the file c:\blah\test.html does not exist. 
RewriteCond  c:\$1\$2           !-f
RewriteRule  ^/([^/]+)/(.*)$    -  [L]

RewriteCond  c:\$1\$2            -f
RewriteRule  ^/([^/]+)/(.*)$    /override-$1/$2    [L]

There are also conditions for testing the existence of a directory. Check the IIRF doc for details.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.