I've got a website which will have countless pages inside a directory. Let me store individuals files on-disk in a lot of subdirectories in line with the first figures from the page title.

For instance http://mysite.com/hugedir/somefile.html

could be saved in /var/www/html/hugedir/s/o/m/e/f/ile.html

That's fairly trivial related to a RewriteRule like so:

RewriteRule ^hugedir/(.)(.)(.)(.)(.)(.*).html   /hugedir/{$1}/{$2}/{$3}/{$4}/{$5}/$6.html
RewriteRule ^hugedir/(.)(.)(.)(.)(.*).html      /hugedir/{$1}/{$2}/{$3}/{$4}/{$5}.html
RewriteRule ^hugedir/(.)(.)(.)(.*).html         /hugedir/{$1}/{$2}/{$3}/{$4}.html
RewriteRule ^hugedir/(.)(.)(.*).html            /hugedir/{$1}/{$2}/{$3}.html
RewriteRule ^hugedir/(.)(.*).html               /hugedir/{$1}/{$2}.html
RewriteRule ^hugedir/(.*).html                  /hugedir/{$1}.html

However, the file title could have hyphens or any other non-standard figures and I'd really prefer to avoid getting a directory named having a strange character. Ideally, Let me have a listing of 'approved' figures and only eliminate or transform the unapproved figures for an underscore.

Can anybody think about a method to do this? Or something like that else equivalent? Area of the requirement is the fact that these be physical files on disk also it 't be parsed having a scripting language.

Apache mod_rewrite enables you to definitely specify an exterior program to create the mapping. (Look for "Exterior Spinning Program"). You could do this it in Perl, for instance.

For instance:

$| = 1;
while (<STDIN>) {
     $dir= $_ . "_________";
     $file = $_;
     $dir =~ tr/a-zA-Z0-9/X/c;
     $dir =~ s!^(.)(.)(.)(.).*!$1/$2/$3/$4!;
     print "$dir/$file\n";

By changing figures into underscores, you'll encounter issues with collisions. For instance, --a and -=a would both be changed into _/_/a.

An easy method of coping with the issue could be getting away the figures using [cde] and also the builtin RewriteMap function: