Tom
Tom

Reputation: 3034

How to redirect to a file when it exists but in a directory?

How can I redirect webbrowsers asking for:

http://example.com/ico/my_ico.png

to

http://example.com/ico/_cache/my_ico.png

when the icon exists in the _cache directory, and redirect to:

http://example.com/ico/index.php?page=my_ico.png

when the requested file does not exist in the _cache directory?

I tried this but it keeps going to the index.php and never to the _cache dir:

Options +FollowSymlinks
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !index.php
RewriteRule ^(.*)$ _cache/$1 [QSA,L]
RewriteRule ^(.*)$ index.php?page=$1 [QSA,L]

Upvotes: 1

Views: 24

Answers (1)

anubhava
anubhava

Reputation: 785246

You can use this code in a /ico/.htaccess file:

Options +FollowSymlinks
RewriteEngine On
RewriteBase /ico/

RewriteCond %{DOCUMENT_ROOT}/ico/_cache/$1 -f [NC]
RewriteRule ^(.+)$ _cache/$1 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?page=$1 [L]

Upvotes: 1

Related Questions