Forum Moderators: phranque
RewriteCond %{REQUEST_URI} ^/([0-9])([A-Za-z0-9\-_+%])*\.jpg$
RewriteRule ^([0-9])(.*) /images/1images/$1$2 [L]
And then I plan to add:
RewriteCond %{REQUEST_URI} ^/([A-Za-z])([A-Za-z0-9\-_+%])*\.jpg$
RewriteRule ^([A-Za-z])(.*) /images/$1images/$1$2 [L]
I tested it locally, and it works OK, but I have a feeling I'm overlooking something. The original issue is that this client's site has tens of thousands of files in the root directory and it's a terrible load on the server. This is a bridge to a redesign, so it doesn't absolutely need to account for any possible future variations in file names. Those are pretty much fixed for now. Still, any comments or corrections would be very helpful. Thanks!
Note the nested parentheses ($1 now includes $2, simplifying the substitution), and the use of [NC] to make the pattern match case-insensitive, thus reducing the required number of range comparisons by one-third. The subpattern for the "filename" has also been simplified to accept anything other than a period or a slash, again for performance.
I also took the liberty of inserting a separator underscore in the /x_images subdirectory path to improve readability and avoid confusion during directory maintenance.
RewriteRule ^(([0-9a-z])[^./]+\.jpg)$ /images/$2_images/$1 [NC,L]
RewriteRule ^(([0-9a-z])[^./]+\.jpg)$ /images/$2/$1 [NC,L]
Jim
OK, I see where you're going with the "/1_images" directory. While I'd advise using a completely-consistent mapping approach, you could indeed do that with two rules:
RewriteRule ^([0-9][^./]+\.jpg)$ /images/1_images/$1 [L]
RewriteRule ^(([a-z])[^./]+\.jpg)$ /images/$2_images/$1 [NC,L]
[edited by: jdMorgan at 4:18 pm (utc) on Mar. 21, 2009]