1

I was wondering if it was possible to write some .htaccess page that makes the server use an index.php file in a SINGLE directory as the index file for every directory/sub-directory on my server, rather than placing the exact same index.php in 200+ directories. If my description isn't clear, what I essentially mean is: /files/index.php is to be used as the index for, for example, /files/morefiles, as well as the index for all directories and sub-directories within /files/, even though those directories would not have an index file themselves. Thanks to all in advance.

EDIT: The php file generates different output based on the directory; To be specific, it lists the contents of the current directory if a certain cookie is set. So, unfortunately, URL rewriting won't work.

2 Answers2

0

Easiest would be a simple .htaccess in the 'main' dir:

RewriteCond %{REQUEST_FILENAME} -d 
RewriteRule ^(*.)$ index.php [QSA]

RewriteRule (^|/)?index.php index.php [QSA]

If you need to switch working dir, in index.php:

<?php
   chdir(realpath(dirname($_SERVER['DOCUMENT_ROOT'].parse_url($_SERVER['REQUEST_URI'],PHP_URL_PATH))));
?>

With possibly some checks if you're still in you /files directory or lower.

Wrikken
  • 981
  • 9
  • 21
0

Just rewrite every path that ends with a / to the index.php.

Something like that:

RewriteBase /files
RewriteRule /$ index.php
Marian
  • 278
  • 1
  • 4
  • I probably should have mentioned that the php file generates different output based on the directory; To be specific, it lists the contents of the current directory if a certain cookie is set. Unfortunately this method doesn't work as the index file always behaves as if it's located in /files. Thanks, though. –  Jun 07 '10 at 21:43
  • You can, just use the `$_SERVER` variables, `REQUEST_URI` should do the job, however there are other variables that could also be used, just do a `print_r` on `$_SERVER` and try out what fits your needs. – Marian Jun 07 '10 at 22:36