使用htaccess mod_rewrite将目录/文件夹拆分成小目录

I'm terrible with mod_rewrite, and not even sure if it is my best method.

i have an /image directory with 20,000 or so images in it. My webhost says they will deactivate my account unless I get each directory under 2500 files.

So I want to change it so that /images/apple.jpg redirects to /images/a/apple.jpg, and /images/banana.jpg redirects to /images/b/banana.jpg so that whenever a php file calls /images/apple.jpg it will redirect. Conversely, any new images created need to be put in the correct directory.

I'd like to code the PHP to do this, but my image directory is referenced in 736 files.

if anyone has better ideas - I'm open to ideas.

Redirecting in .htaccess to the new directories doesn't seem like the best idea as you need at least 8 different directories (And 8 different rewrite rules) at this moment to split the 20000 files into directories with 2500 files maximum. So, tomorrow 9 directories will be needed, not mentioning that it might become really difficult with rewrite rules to select the files that go to each directory .

I guess the best option is to handle that routing in a PHP script, with arrays or whatever is needed.

You could redirect everything that goes to the /images directory to a php script in root, for example, that in turn redirects to the appropriate directory according to the parameters passed to the script

Like this:

Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI}  !handler.php
RewriteCond %{REQUEST_URI} ^/images/([^\.]+)\.([\w]+)/?  [NC]
RewriteRule .*    handler.php?name=%1&ext=%2    [L]

Maps silently:

http://example.com/images/filename.ext with or without trailing slash.

To:

http://example.com/handler.php?name=filename&type=ext

To pass only one parameter to the script, instead or the name and the extension, just replace the rewrite rule with this one:

RewriteRule .*    handler.php?%1.%2    [L]

This is just an example. The script handler can have any name and so the keys name and type, if used.

The incoming URL structure has to be kept for the rule-set to work: Last string inside the URL must be the file.

For permanent and visible redirection, replace [L] with [R=301,L].