Madan Sapkota
Madan Sapkota

Reputation: 26081

Apache .htaccess image exists check and redirect

I have a website with file directory like this

/index.php (home page)

/storage (file storage directory)
    /800 (800 pixel width dir)
        /800x200.png
        /800x350.png
        ....
    /200 (200 pixel width dir)
        /200x150.png
        /200x185.png
        ...
    ....

/css
    /style.css

/images
    /logo.png

/jscript
    /autoload.js

Now user will make a request http://example.com/images/200x150 or http://example.com/images/200x180. From two URL we know that first image is existed in /storage/200/200x150.png but not second one.

So, I want to write .htaccess for this (theoretically here).

Rewrite Condition /storage/{width}/{widthxheight}.png existed?
Rewrite Rule {output the image}
Rewrite Failed {go to /somedir/failed.php}

How can I do this?

Upvotes: 2

Views: 1674

Answers (2)

Felipe Alameda A
Felipe Alameda A

Reputation: 11799

From your examples, the typical URL for an image request is like this

http://example.com/images/WidthxHeight

where Width and Height are variables and images is a fixed string.

And the typical substitution URL should be like this:

http://example.com/storage/Width/WidthxHeight.png

Where Width and Height are the parameters passed from the incoming URL, while storage and png are fixed string.

You may try this:

Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /

# Make sure the request is for an image file
RewriteCond %{REQUEST_URI}  ^/images/([^x]+)x([^/]+)/?   [NC]

# Don't want loops
RewriteCond %{REQUEST_URI}  !storage                     [NC]

# Make sure the file exists
RewriteCond %{REQUEST_FILENAME}    -f

# If all conditions are met, rewrite
RewriteRule .*   /storage/%1/%1x%2.png                   [L]

## Else, map to failed.php
RewriteCond %{REQUEST_URI}  !storage                     [NC]
RewriteCond %{REQUEST_URI}  !failed\.php                 [NC]
RewriteRule .*   /somedir/failed.php                     [L]

UPDATE

With 2 additional rules for incoming URLs with one parameter and no parameter.

Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /

## New option 1
## Check if the request has any parameter
RewriteCond %{REQUEST_URI}  !storage                   [NC]
RewriteCond %{REQUEST_FILENAME}    -f
RewriteRule ^images/?$   /storage/200/200x200.png      [L,NC]

## New option 2
## Check if the request has only 1 parameter
RewriteCond %{REQUEST_URI}  !x                          [NC]
RewriteCond %{REQUEST_URI}  !storage                    [NC]
RewriteCond %{REQUEST_FILENAME}    -f
RewriteRule ^images/([^/]+)/?$   /storage/$1/$1x$1.png  [L,NC]

## Check if the request has 2 parameters
RewriteCond %{REQUEST_URI}  !storage                    [NC]
RewriteCond %{REQUEST_FILENAME}    -f
RewriteRule ^images/([^x]+)x([^/]+)/?  /storage/$1/$1x$2.png  [L,NC]

## Else, map to failed.php
RewriteCond %{REQUEST_URI}  !storage                     [NC]
RewriteCond %{REQUEST_URI}  !failed\.php                 [NC]
RewriteRule .*   /somedir/failed.php                     [L]

For permanent and visible redirection, replace [L,NC] with [R=301,L,NC]

Upvotes: 1

Emery King
Emery King

Reputation: 3534

depending on how your .htaccess is currently setup, you could route all non existing files to failed.php:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . /failed.php [L]

Upvotes: 0

Related Questions