Search code examples
iismod-rewriteurl-rewritingiirf

RewriteCond Check if file exists in a subdirectory


I'm using Iirf v2.0.

I have the following directory structure:

/
/library
/library/index.php
/webroot
/webroot/images
/Iirf.ini

Where I have a library folder which contains my application, a webroot folder (which contains images, stylesheets etc) and an Iirf.ini config file.

I'm wanting to redirect all requests to /library/index.php if the file doesn't exist under webroot.

eg:

Request             Response
/images/blah.png -> /webroot/images/blah.png
/news            -> /library/index.php

My Iirf.ini config has:

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

Which redirects everything to /library/index.php but I'm having trouble working out how to check if the REQUEST_FILENAME exists under webroot.

I've looked at this question but I don't have access to DOCUMENT_ROOT. It gives me the following (taken from the log):

Thu Jul 15 11:46:21 -   760 - ReplaceServerVariables: VariableName='REQUEST_FILENAME' Value='C:\web\favicon.ico'
Thu Jul 15 11:46:21 -   760 - ReplaceServerVariables: in='%{DOCUMENT_ROOT}/webroot/%{REQUEST_FILENAME}' out='DOCUMENT_ROOT/webroot/C:\web\favicon.ico'

Any help would be greatly appreciated.

--- EDIT --

I've updated my config after more reading and the suggestions of Tim to be:

RewriteCond $0 !^/webroot
RewriteRule ^.*$ /webroot$0 [I]

RewriteCond $0 !-f
RewriteRule ^/webroot/(.*)$ /library/index.php [I,L,QSA]

And it passes to /library/index.php correctly but it still doesn't check for an existing file (even though it seems to say that it does).

Thu Jul 15 14:47:30 -  3444 - EvalCondition: checking '/webroot/images/buttons/submit.gif' against pattern '!-f'
Thu Jul 15 14:47:30 -  3444 - EvalCondition: cond->SpecialConditionType= 'f'
Thu Jul 15 14:47:30 -  3444 - EvalCondition: Special: it is not a file

I think I'm going to have to contact the author of the Filter.


Solution

  • I ended up having to swap to using the Helicon Tech ISAPI_Rewrite 3 filter.

    The htaccess file I ended up using was:

    RewriteEngine On
    
    # Check whether the file exists and if not, check whether the request starts
    # with webroot. Prepend webroot if it doesn't.
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_URI}      !^webroot
    RewriteRule ^.*$ webroot/$0 [NI]
    
    # Check whether the file exists, if not, send the request off to library/index.php
    RewriteCond %{DOCUMENT_ROOT}/$0 !-f
    RewriteRule ^(webroot/)?(.*)$ library/index.php [I,L,QSA]