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?

  • Not a duplicate, that already presumes `mod_rewrite` is a given, and the question is about its use. – MSalters Mar 10 '14 at 12:56
  • @MSalters For this instance, it is, or at least it should be. – Jenny D Mar 10 '14 at 15:40
  • @JennyD: That's a valid answer, not a reason to close as duplicate. You may of course refer in your answer to the existing answer for implementation details, but please also include the reason _why_ `mod_rewrite` is the correct solution. – MSalters Mar 10 '14 at 15:58

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
  • 12,830
  • 3
  • 31
  • 40