我的站点使用PLESK服务器。我最近将文档根目录从httpdocs
更改为httpdocs/public
。这是为了增加私人使用文件的安全性。
但是,我发现它没有正确重定向。例如。我在公共目录中有一个index.php,现在它会自动重定向到admin目录。让我向您介绍如何确定某些常量,然后介绍用于重定向的两个函数。
以下定义的常量很少:
<?php
// Assign file paths to PHP constants
// __FILE__ returns the current path to this file
// dirname() returns the path to the parent directory
define("PRIVATE_PATH", dirname(__FILE__));
define("PROJECT_PATH", dirname(PRIVATE_PATH));
define("PUBLIC_PATH", PROJECT_PATH . '/public');
define("SHARED_PATH", PRIVATE_PATH . '/shared');
define("ARRAY_PATH", PRIVATE_PATH . '/arrays');
// Assign the root URL to a PHP constant
// * Do not need to include the domain
// * Use same document root as webserver
// * Can dynamically find everything in URL up to "/public"
$public_end = strpos($_SERVER['SCRIPT_NAME'], '/public') + 7;
$doc_root = substr($_SERVER['SCRIPT_NAME'], 0, $public_end);
define("WWW_ROOT", $doc_root);
然后这是我的functions.php中包含在initialize.php中的两个函数(上面的片段来自initialize.php)
<?php
/**
* @param string $script_path
* @return string
*/
function url_for(string $script_path): string
{
// add the leading '/' if not present
if ($script_path[0] != '/') {
$script_path = "/" . $script_path;
}
return WWW_ROOT . $script_path;
}
/**
*
* @param string $loc
*/
function redirect_to(string $loc): void
{
header("Location: " . $loc);
exit;
}
下面是预先提到的index.php
<?php
require_once('../private/initialize.php');
header('Location: ' . url_for('admin'));
请注意,在这种情况下,我没有使用redirect_to()函数。这是我得到的输出网址:
这是无效的,并引发404 http错误。因此决定看看如果我转到原本期望的网址
会发生什么情况。找到页面,但找不到我项目的css和js文件。有趣的是,我看到了包含它们的证据,因为css和js是通过SHARED_PATH常量从私有目录动态加载的。那我为什么看到这个索引。我将如何停止它?这是我的基本问题。