I am trying to create a permanent (301) redirect all the files in a directory to a single file within that directory.

For instance, if a user visits site.com/directory/randompage.html, I want them to be redirected to site.com/directory/index.html.

I have tried putting a redirect on the directory and then overriding the redirect on the file level, but IIS does not appear to allow this.

Is there a way to accomplish what I am after? or do I have to create a redirect on a file by file basis?

  • What version of IIS are you using? – squillman Jul 24 '09 at 21:04

For attempted access to files that don't exist you can put a custom 404 error on the directory that redirects the user to the URL for the file you'd like them to access.

If there are other files in that directory you'll have to put redirects on them on a file-for-file basis. That's just the way it is, unfortunately. If you have a lot of files you could script the metabase modification to add the redirect.


If you can modify the NTFS ACLs on all those files to disallow the user from reading them, you could also drop a custom 401.3 error page on the directory, too.

If not, then try this:

@echo off
SET SOURCE_DIR=C:\path-to-directory-in-filesystem
SET ADSUTIL=cscript C:\inetpub\adminscripts\adsutil.vbs
SET METABASE_PATH=W3SVC/####/ROOT/path/to/site/directory
SET REDIRECT_URL=/foo/foo.txt

for /f "usebackq delims=" %%i in (`dir /b %SOURCE_DIR%`) do (

Set the SOURCE_DIR to the physical path in the filesystem of the directory with the files to be made into redirects. ADSUTIL is probably set right if you're a stock IIS 6 install. METABASE_PATH needs to refer to the site identifier number (### in the sample, get it from the IIS management console for your site) and the path under the root (leave /ROOT/ there-- that needed). Finally, REDIRECT_URL needs to be the URL (can be absolute or relative to the root of the site).

  • Thanks! There are a few thousand files unfortunately. Can you direct me to a decent resource for scripting the metabase modifications? – cbeckner Jul 24 '09 at 20:27
  • There-- have at. >smile< – Evan Anderson Jul 24 '09 at 20:54

You should be able to employ a URL rewrite module to do this. There are 2 that I know of for IIS6 and earlier, IIS7 has it's own module (finally).

You can have a look at Helicon's ISAPI_Rewrite or Ionics Isapi Rewrite (also works with IIS7). I use Ionics' because it's free, but Helicon's rewrite rules I believe map directly with the mod_rewrite Apache module's rules which is a big win for it in general.

Both have the ability to provide permanent redirects and the rules are regular expression based. I'll see if I can put together the syntax for a rule that would cover what you want to do and post it shortly.


Another option (just to have it out there), is you could tell IIS to let ASP.Net handle .html in that site, then create a global.asax file with Application_BeginRequest code that checks the URL and redirects accordingly.


All that metabase modification- thousands of files you say? You could do this with one wildcard rule in IIRF.

RedirectRule ^/directoryToRedirect/(.*)(?<!index\.html)$  /index.html  [L]

In english, it says, for any URL in the directory directoryToRewrite, which is NOT index.html, redirect it to index.html .

This works regardless of how many files you have, whether you add new files, remove files, whatever.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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