.htaccess DirectoryIndex文件夹中的第一个HTML文件

时间:2012-08-17 22:10:00

标签: .htaccess

我有一个目录,其中只有一个.html文件,但我不知道它将被调用什么,因为它是由我无法控制的引擎检索的。

如何将.htaccess DirectoryIndex指向它遇到的第一个(也是唯一的).html文件?

1 个答案:

答案 0 :(得分:1)

DirectorIndex到index.php并写一个小PHP脚本,执行以下操作。这将扫描目录中的第一个HTML文件并重定向到该文件。

<?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();
相关问题