Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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.

share|improve this question

2 Answers 2

up vote 0 down vote accepted

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

Something like that:

RewriteBase /files
RewriteRule /$ index.php
share|improve this answer
    
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. –  whatisthis Jun 7 '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 7 '10 at 22:36
    
Thanks, I'd completely forgotten about $_SERVER. –  whatisthis Jun 9 '10 at 20:06

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.