0

lets say i make a request to http://www.mysite.com/files/picture.jpg so my picture.jpg is supposed to be in htdocs/files/picture.jpg

how do i configure if picture.jpg does not exist in htdocs/files then look it up inside htdocs/files/alt?

the filename varies, but the alternate directory name (alt) is fixed (which reside inside the original directory)

is this achievable using apache config?

1 Answers1

0

You should use an Apache module called mod_rewrite to achieve this. http://httpd.apache.org/docs/current/en/rewrite/

Apache's mod_rewrite module provides a way to modify incoming URL requests dynamically, based on regular expression rules.

The RewriteCond directive from mod_rewrite has the -f flag which allows you to test if a file exists.

To enable this module you should just have to run the following and restart Apache :

# a2enmod rewrite
# service apache2 restart

Then, you will have to define a Rewrite rule in your VHost config file. The Rewrite rules will be something like this :

RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
RewriteRule ^/files/(.+)$ /files/alt/$1 [L]
krisFR
  • 13,690