Alfo
Alfo

Reputation: 4819

.htaccess DirectoryIndex first HTML file in folder

I have a directory in which there will only ever be one .html file, but I don't know what it will be called, as it is retrieved by an engine that I have no control over.

How can I make the .htaccess DirectoryIndex point to the first (and only) .html file it comes across?

Upvotes: 1

Views: 195

Answers (1)

TerryE
TerryE

Reputation: 10888

DirectorIndex to index.php and write a small PHP script which does the following. This will scan the directory for the first HTML file and redirect to that.

<?php
foreach( scandir('.') as $f ) {
    if ( preg_match( "/\.html$/", $f) == 1  ) {
        header( "Location: http://{$_SERVER['HTTP_HOST']}/uri_path/$f", true, 302 );
        exit();
    }
}
header( 'Status: 404');
exit();

Upvotes: 2

Related Questions