Rewrite rules - if file exists in different location

Discussion in 'Apache' started by grutland, Jun 24, 2013.

  1. #1
    Hello,
    Need a bit of help with a rewrite rule.
    I've got the basics which I've developed from various bits and pieces over the years but facing a new problem which I can't quite work out.

    RewriteEngine on
     
    RewriteRule ^index\.php$ - [L]
     
    RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} -f [OR]
    RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} -d
    RewriteRule ^ - [L]
     
    RewriteRule . /index.php [L]
    Code (markup):
    After the test that checks a file/directory exists I need to do a similar thing, but check a different directory.
    In my head it would work something like this.

    RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} -f [OR]
    RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} -d
    RewriteRule (.*) /dynamic-folder/$1 [L]
    Code (markup):
    But the part I can't work out is how to check the specified directory in the rewrite rule.
    I wonder if it's something along these lines.

    RewriteCond %{DOCUMENT_ROOT}/dynamic-folder/%{REQUEST_FILENAME} -f [OR]
    RewriteCond %{DOCUMENT_ROOT}/dynamic-folder/%{REQUEST_FILENAME} -d
    RewriteRule (.*) /dynamic-folder/$1 [L]
    Code (markup):
    So the final result would be something like this.

    RewriteEngine on
     
    RewriteRule ^index\.php$ - [L]
     
    RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} -f [OR]
    RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} -d
    RewriteRule ^ - [L]
     
    RewriteCond %{DOCUMENT_ROOT}/dynamic-folder/%{REQUEST_FILENAME} -f [OR]
    RewriteCond %{DOCUMENT_ROOT}/dynamic-folder/%{REQUEST_FILENAME} -d
    RewriteRule (.*) /dynamic-folder/$1 [L]
     
    RewriteRule . /index.php [L]
    Code (markup):
    Any help would be greatly appreciated.
     
    grutland, Jun 24, 2013 IP