views:

382

answers:

2

I am trying to redirect to a subdirectory for files, but only if the file exists in the subdirectory. Essentially, its modeled like this:

  1. Does the file exist regularly? If so, display it.
  2. Does the file exist in the subdirectory? If so, redirect to it.
  3. If we're at this point, route into a dynamic script at index.php.

The tough part has been step 2. Here's what I've tried to far:

RewriteEngine On
RewriteCond engine/%{SCRIPT_FILENAME} -f [OR]
RewriteCond engine/%{REQUEST_FILENAME} -d
RewriteBase /lab
RewriteRule ^(.*)$ /lab/engine/$1  [P]
+1  A: 
# check if the file exists
#   ...and if found, stop and display it
RewriteCond  %{DOCUMENT_ROOT}/engine/%{SCRIPT_FILENAME}  -f
RewriteRule  ^(.*)$  /engine/$1  [L,QSA]

#   second try to find its in another directory
#   ...and if found, stop and display it
RewriteCond  %{DOCUMENT_ROOT}/lab/%{SCRIPT_FILENAME}  -f
RewriteRule  ^(.*)$  /lab/$1  [L,QSA]

# default
RewriteRule ^(.*)$ index.php [L,QSA]
deepwell
Thanks for the help, but the problem is that SCRIPT_FILENAME actually contains the full filesystem path. Thus, the /engine/ is put in the entirely wrong location in the path.
morgante
then just parse the script filename with a last instance of before doing this.
IPX Ares
You could also try %{REQUEST_URI}
deepwell
A: 

I managed to get it to work using a slightly different method. I first check if the file exists in the subdirectory, otherwise I proceed to the dynamic controller — which includes an exclusion for real files.

DirectoryIndex handle.php

RewriteEngine On

# # check if the file exists in "engine"
# #   ...and if found, stop and display it
RewriteCond  %{DOCUMENT_ROOT}/lab/engine/$1 -f
RewriteRule  ^(.*)$  /lab/engine/$1  [L,QSA]

# If we've gotten here, route to dynamic controller
RewriteBase /lab
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ handle.php [L,QSA]
morgante