RewriteRule only works if folder does not exist

.htaccessapache-2.2rewrite

Instead of directly accessing images, I need to use RewriteRule to redirect a set of images to be accessed via a script.

A RewriteRule is used for it:

RewriteRule ^Images/test/([^.\/]*)\.png /image.php?file=$1.png [R=301,L]

I would expect this to change:

www.example.com/Images/test/123.png

into

www.example.com/image.php?file=123.png

But it does not do it.

But If I change the RewriteRule to the following:

RewriteRule ^Images2/test/([^.\/]*)\.png /image.php?file=$1.png [R=301,L]

The following works:

www.example.com/Images2/test/123.png

into

www.example.com/image.php?file=123.png

The difference is that Images exsits while Images2 does not exist. Is it possible to it working with an existing folder.

Any help on how to get the top example working?

Update:
After carefully looking through the mod_rewrite documentation, I found this:

…Note that mod_rewrite tries to guess whether you have specified a file-system path or a URL-path by checking to see if the first segment of the path exists at the root of the file-system…

It seems that if the path is interpreted as a URL-path it works and if it is interpreted as a file-system path it fails.

Best Answer

try with this

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$0 [L]

luck!!

Related Topic