There are some files (with unique names) in a directory structure like this:
/var/www/domain1/unique_filename1
/var/www/domain2/unique_filename2
/var/www/domain3/unique_filename3
At the moment, all of the domain directories are accessible as sub-directories of a master domain and clients can access the files using following URIs:
www.my-master-domain.com/domain1/unique_filename1
www.my-master-domain.com/domain2/unique_filename2
www.my-master-domain.com/domain3/unique_filename3
I'd like to modify Apache 2.4 configuration so that I will be able to access files without mentioning secondary domains in my URLs, for example:
www.my-master-domain.com/unique_filename1
www.my-master-domain.com/unique_filename2
www.my-master-domain.com/unique_filename3
And of course it's not desirable to do any redirect on client browsers (and event If I wanted, it would not be possible in this special case), I just need to directly server the files via second set of mentioned URLs.
Any ideas?
You could create a
RewriteMap
that maps "unique" filenames to the domain it is stored under. You can then use aRewriteRule
directive to lookup the domain from the requested "unique" filename and internally rewrite the request.For example, in your server config, define the
RewriteMap
:Then, in
/var/www/filename-to-domain-map.txt
you would include all your filename mappings:Now you can use mod_rewrite to reference this map in order to internally rewrite a request from
/unique_filename2
to/domain2/unique_filename2
(for example).This assumes that "unique_filename" is exactly that; just a filename. No additional URL-path.
You could make the lookup more efficient by using a
dbm:
DBM Hash File (orfastdbd:
SQL Query?) instead of a plain text file, if you have many "filenames".Reference:
https://httpd.apache.org/docs/2.4/rewrite/rewritemap.html
Alternatively, you could perform filesystem checks for the existence of "unique_filename" in each domain subdirectory. The first match wins. This might be OK if you only have 2 or 3 domain subdirectories, but if you have many then this might become a performance problem as filesystem checks are relatively expensive. For example, again using mod_rewrite: