I think you could do this to replace one slash at a time with an underscore:
RewriteRule ^/(.+)/(.*?)$ /$1_$2 [N]
RewriteRule ^.*$ /stuff$0
The [N]
flag causes Apache to restart the URL rewriting process from the first rule. If you have other rules that apply to the URL before these two, remember that they'll be invoked on every iteration as well.
An alternative, which may or may not be more efficient, would be to use an external program to handle the rewriting. You'd put this in your Apache configuration file (instead of the above)
RewriteMap slashtouscore prg:/usr/local/bin/slashtouscore.pl
RewriteRule ^/(.*)$ /stuff${slashtouscore:$1}
and then you'd need to create the executable script /usr/local/bin/slashtouscore.pl
with the contents
#!/usr/bin/perl
$| = 1;
while (<>) {
s|/|_|g;
print;
}
(It doesn't have to be a Perl script, of course, any program with the same function will do - and of course the filename can be whatever you want, as long as it's accessible to Apache)
Note that none of this is tested.