Replace mod_autoindex with PHP indexer in all directories without adding a index.php to all of them

StackOverflow https://stackoverflow.com/questions/9944912

  •  28-05-2021
  •  | 
  •  

Frage

I like the ability to automatically index a folder, so that I can serve a large number of files, without adding links to a page all the time. However I dislike the httpAuth login box that comes with using .htaccess to secure a directory. Plus there are more features I wanted on my indexes. So I have written a PHP script to generate indexes the way I want, so that I can control everything with PHP, store users in SQL, add extra links to my file editor, and log in with a nice looking web form.

The problem is any new directory needs an index.php file that includes the script, or I just get apache indexes. Which means copying a one line index.php file to every directory. I could generate it using PHP, but if I am working with FTP to manage files that will not solve all the problems. Is there any way to configure apache to display my index script in any directory that does not have an index files of its own? Such that it acts just like mod_autoindex? But with my custom script.

War es hilfreich?

Lösung

Put the index.php in your root, then use redirect

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -d #if directory exists
RewriteRule . /index.php [L]

Then inside index.php use $_SERVER['REQUEST_URI'] to figure out which folder was requested, then use PHP to display what you wanna display, e.g. using opendir, readdir, etc (don't forget to handle the '.' and '..' unix files)

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top