mod_rewrite: rewrite URLs using sub-string of file names for directory paths?

I’m trying to use Apache mod_alias / mod_rewrite / scriptaliasmatch to solve the following problem:

I need requests for files in this format:

//mysite.com/images/filename.jpg

to be mapped to files that exist at this location:

//mysite.com/images/f/i/filename.jpg

The first two letters of the file name need to be added to the directory path of the file.

I need this to work for all requests to the images folder, so these URL’s

//mysite.com/images/dog.jpg
//mysite.com/images/cat.jpg
//mysite.com/images/bird.jpg

Need to be mapped to the files:

//mysite.com/images/d/o/dog.jpg
//mysite.com/images/c/a/cat.jpg
//mysite.com/images/b/i/bird.jpg

What is the best way to accomplish this?

You can capture parts of the original string with (parentheses), then you can use those captured values in the replacement with $1, $2, etc. Here’s an example that will probably work. I haven’t tested it.

RedirectMatch 301 ^/images/(([^/])([^/])[^/]*)$ /images/$2/$3/$1

Thanks so much Jeff … This worked perfectly once I removed the first forward slash!