php – 如何重定向Apache“Index of …”页面?
发布时间:2020-12-13 16:45:40 所属栏目:PHP教程 来源:网络整理
导读:我试图通过Apache的 autoindex module使用PHP在Apache中实现经过身份验证的文件列表. 我想象的方式是让Apache运行一个PHP脚本作为header file.我已经设法让Apache为头文件正确运行PHP,它也检测到登录cookie.但似乎Apache将头文件作为单独的请求运行,这意味着
我试图通过Apache的
autoindex module使用PHP在Apache中实现经过身份验证的文件列表.
我想象的方式是让Apache运行一个PHP脚本作为header file.我已经设法让Apache为头文件正确运行PHP,它也检测到登录cookie.但似乎Apache将头文件作为单独的请求运行,这意味着如果我尝试从PHP发送重定向头,则它不会运行. 我的(简化的)Apache配置: DocumentRoot "/path/to/files_root" Alias /~extra "/path/to/extra-data" <Directory "/path/to/extra-data"> Options -Indexes -MultiViews +Includes AllowOverride None Order allow,deny Allow from all </Directory> IndexOptions FancyIndexing HTMLTable SuppressHTMLPreamble AddType text/html .php .html .htm AddOutputFilter INCLUDES .php AddHandler application/x-httpd-php .php HeaderName "/~extra/HEADER.php" 我的HEADER.php文件: <?php if ( ! my_validate_cookie_function()) { header('HTTP/1.1 302 Found'); header('Location: http://login.example.com/'); exit(1); } 因此,标头不会发送到浏览器.设置Apache环境viariables似乎不起作用,因为它们在HEADER.php完成执行的那一刻就已经过去了. cookie本身是加密的,因此需要PHP来验证它. 有什么建议如何达到预期的效果? 解决方法
您应该使用< body>中的以下代码将index.php文件插入到您的目录中.标签.
function fileindex($folder) { if (!is_dir($folder)) { return array(); //empty if not a folder } $list = scandir($folder); array_shift($list); //first two values are always . & .. array_shift($list); return $list; } /* auth stuff here */ if (is_auth) { echo "<h1> Index of ".getcwd()."</h1>n<ul>"; echo "n<li><a href="/">Parent Directory</a>"; foreach (fileindex(".") as $i) { echo "n<li><a href="".$i."">".htmlentities($i,ENT_QUOTES|"ENT_HTML401","UTF-8",true)."</a></li>"; } echo "</ul>"; } 既然你告诉我你不能使用index.php,你应该使用Apache将目录重定向到wherever / other.php?directory = path并从那里开始工作. 在.htaccess中,解决方案就是 RewriteCond %{REQUEST_URI} -d RewriteRule ^(.*)$wherever/other.php?directory=$1 [L] 然而,最值得注意的是,您需要稍微编辑PHP代码以适应作为$_GET参数的文件夹,而不是getcwd()和fileindex(“.”). (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |