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:




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:




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.


try with this

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


| improve this answer | |

Try changing the beginning of your anchor to ^/, just a hunch. I have not personally used RewriteRules anchored to the beginning of the line that do not begin with a slash, but I can see how that might create some weirdness within the engine.

| improve this answer | |
  • 1
    It depends on the setting of RewriteBase. – Michael Hampton Jan 24 '13 at 17:35
  • I have tried this, but it did not help. RewriteBase is set to the root of the apache2 www folder. – Badran Jan 24 '13 at 18:12
  • @user155908 Just to make sure I'm not misunderstanding you, RewriteBase should be a URI and not a filesystem path. (if you meant that it's set to /, you're fine) – Andrew B Jan 24 '13 at 18:22
  • @Andrew B It is set to /. – Badran Jan 24 '13 at 18:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.