访问包含索引文件的文件夹的父目录中的文件

问题描述 投票:0回答:1

目录如下: 文档

  • 公众

    • index.php
  • 欢迎.php

“public”文件夹被设置为文档根目录,以便传入请求转到index.php。 index.php 检查用户是否经过身份验证,如果没有,它会重定向将用户发送到欢迎页面。

header("Location: welcome.php");

但是,尽管 URL 显示了正确的文件路径,但我仍然收到 404 错误。

有什么想法吗?这是我尝试过的。

header("Location: /welcome.php");
header("Location: ./welcome.php");
header("Location: ../welcome.php");

.htaccess

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

^上面创建了太多重定向。

将index.php文件放在与welcome.php相同的文件夹中效果很好,但我正在尝试组织我的网络应用程序,如果我必须将所有内容都保留在index.php文件中或之下,则无法做到这一点。

php apache redirect web-applications http-headers
1个回答
0
投票

似乎您正在使用 Apache 作为网络服务器。你想做的事是不可能的。如果 Apache 中 Web 应用程序的文档根设置为目录

public/
,那么您将只能提供该目录中的文件。由于
welcome.php
public/
处于同一级别而不在其中,因此您无法提供该文件,并且 Apache 会正确地为您提供 404 错误。

相反,请考虑调整你的方法

- public (foo.com)
-- index.php
-- secure
--- index.php
-- welcome.php

当用户登陆 foo.com 时,要求他们登录,登录后将其重定向到 foo.com/secure。如果有人尝试访问 foo.com/secure 但未通过身份验证,请将其标头返回到welcome.php 或index.php。

© www.soinside.com 2019 - 2024. All rights reserved.