keriati
keriati

Reputation: 55

Apache rewrite to other directory, if file exists there

My directories look like this:

/assets
  /images
    /2011
    /2012
  /images-hq
    /2011
    /2012

In the html file every image has a tag like:

<img src="/assets/images/2012/example.jpg" />

I want to check, if the same image exists in the images-hq folder, and if it is there, i want to send that image to the user.

Example:

/assets/images/2012/example.jpg to /assets/images-hq/2012/example.jpg

IF /assets/images-hq/2012/example.jpg exists, if not, just serve the original.

Upvotes: 2

Views: 1225

Answers (1)

Chris
Chris

Reputation: 333

I tried doing this and seems to work:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} (.*)/[a-z]+/(.+)\.(gif|jpe?g|png)$
RewriteCond %1/images-hq/%2.%3 -f
RewriteRule (.*)/[a-z]+/(.+)\.(png|jpg|jpeg)$ %1/images-hq/%2.%3 [L]

I based on this solution

Upvotes: 5

Related Questions