Forum Moderators: phranque

Message Too Old, No Replies

.htaccess (mod rewrite) rewrite is searching for a file

A RewriteRule is matching a file, even without an extension

         

jasonjewels

9:48 am on Oct 22, 2010 (gmt 0)

10+ Year Member



Hi everyone.

This problem has me stumped. I hope someone here can help.

I have the following rule in my .htaccess:

 RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^images/(.*)$ resources/imgs/$1 [QSA,L]


It allows for a fake 'images' directory. For example:

 http://www.blahblah.com/images/test.jpg
actually points to
http://www.blahblah.com/resources/imgs/test.jpg


It works fine, except for the following cases:

 http://www.blahblah.com/images/test
http://www.blahblah.com/images/test.


They also work when they shouldn't! 'test' and 'test.' don't exist, but test.jpg does.

I thought perhaps the rewriting was doing some type of pattern match and finding the first file, but the following doesn't work:

 http://www.blahblah.com/images/test.j


I enabled logging and found the following:


127.0.0.1 - - [22/Oct/2010:19:48:44 +1100] [localhost/sid##*$!#*$!x][rid##*$!#*$!x/initial] (2) [per-dir C:/htdocs/] rewrite images/test -> resources/imgs/test
127.0.0.1 - - [22/Oct/2010:19:48:44 +1100] [localhost/sid##*$!#*$!x][rid##*$!#*$!x/initial] (2) [per-dir C:/htdocs/] trying to replace prefix C:/htdocs/ with /
127.0.0.1 - - [22/Oct/2010:19:48:44 +1100] [localhost/sid##*$!#*$!x][rid##*$!#*$!x/initial] (1) [per-dir C:/htdocs/] internal redirect with /resources/imgs/test [INTERNAL REDIRECT]
127.0.0.1 - - [22/Oct/2010:19:48:44 +1100] [localhost/sid##*$!#*$!x][rid##*$!#*$!x/initial/redir#1] (1) [per-dir C:/htdocs/] pass through C:/htdocs/resources/imgs/test.jpg


The last line seems to be the culprit, but I have no idea why.

Does anyone know how to stop it from 'finding' the actual file when it should bring up an error (404, etc)?

Thanks!

jdMorgan

11:45 am on Oct 22, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This is not due to mod_rewrite, which is a quite deterministic module...

It is likely due to content-negotiation (see mod_negotiation). Similar behavior can also be seen (under slightly different circumstances from yours) due to AcceptPathInfo being enabled.

Such behavior can also be seen (making a smaller degree of requested-path changes) with mod_speling. Mod_Alias directives in the server config files can also change the requested path information. When investigating a problem like this, the directives of all modules capable of doing URL rewriting may have to be checked -- even mod_dir can get involved to add a trailing slash to directory paths.

Because of the exact nature of the "mismatch" of your requested URL to the correct one in this specific case, I'll bet on content-negotiation. Try adding an Options directive (or modify your existing one) to disable it.

Add:
 Options -MultiViews 


Or modify your existing directive:
 Options +FollowSymLinks -Indexes -MultiViews 


This latter is an example only. I suggest that you look up the "Options" directive in the Apache core directives section, and configure the ones that you want and need.

Jim

jasonjewels

11:22 pm on Oct 22, 2010 (gmt 0)

10+ Year Member



I've always seen MultiViews and never bothered looking into it. VERY interesting stuff. It was indeed content-negotiation selecting the 'best' case.

Thanks for the help Jim