There's a piece of my website that's setup for simple posting/discussing of files, every time a file is submitted it's placed within it's own directory, good examples:

I am curious if Apache could be set up so that any demands to some file's parent directory will instantly be rerouted towards the file itself:  -> ->

I understand a person .htaccess file might be produced within each directory which may clearly result in the redirect but I'd rather not have access to millions of .htaccess files for every of my million submitted files.


I do not think you can do this with stock Apache. mod_rewrite may be the tool you'd normally use, but to the very best of my understanding it does not have facilities for listing files inside a directory.

You can write just a little spend script to complete the redirect (ex, something similar to:

cd "$base_dir"
echo "301 Moved permanently"
echo "Location: /$(ls -1 "requested" | head -n 1)"

but that is only a quick hack, and most likely does not work), then make use of a mod_rewrite rule like:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -d 
RewriteRule (.*) /cgi-bin/redirect?$1