所以我刚开始创建一个新网站,或者更改一个旧网站 . 所有代码都是一样的,唯一的区别是我在这里和那里改变了一些文字 . 旧的网站工作得很好,所以这个也应该这样,所以我不确定为什么会这样,但只有主页可以使用我想要使用的URL找到 . 例如,尝试去项目页面,不仅找不到文件,而且我的自定义未找到的页面甚至都不显示 . 首先,我将展示一些示例,然后我将展示一些代码 .
-
有效的主页:teivodov.com
-
项目页面适用于旧网站,但不在此网站上:teivodov.com/projects
-
项目页面有效,但丑陋的网址形式https://teivodov.com?page=projects
-
未找到页面仅在页面使用时使用:https://teivodov.com?page=blahblah
这是我的htaccess文件:
//拒绝访问此文件<Files~“.htaccess”>拒绝所有</ Files> // start RewriteEngine RewriteEngine On //如果被调用的文件不是目录,文件或链接,我们调用index.php? page = RewriteCond%!-d RewriteCond%!-f RewriteCond%!-l RewriteRule ^( . *)$ index.php?page = $ 1 [QSA,L] //禁用文件列表选项 - 索引//将错误页面403(无权限)和404(未找到页面)设置到我们未找到的页面ErrorDocument 403 / notfound ErrorDocument 404 / notfound order deny,allow
(#替换为//)
我的index.php:
<?php
function dump_error_to_file($errno, $errstr) {
file_put_contents('/errors.log', date('Y-m-d H:i:s - ') . $errstr, FILE_APPEND);
}
error_reporting(-1);
ini_set('display_errors', 'On');
set_error_handler('dump_error_to_file');
//load needed settings, constants, page array and functions
include 'variables.php';
include 'constants.php';
include 'functions.php';
//setting timezone to America/Chicago, needed by some functions
date_default_timezone_set('America/Chicago');
//gets changed to the return of the include file
$ret = 1;
/*
* The include file has to contain the following values:
* Array('filename' => string, -- Filename for template
* 'data' => Array()) -- Array with data for the template
* - At an exception
* string -- Errormessage.
*/
//if no page was called, include the main page
if (!isset($_GET['page'])) {
$ret = include 'includes/' . $files['main'];
} else {
if (isset($_GET['page'])) {
$page = trim($_GET['page']);
} else {
$page = 'main';
}
if (isset($files[$page])) {
//if the file exists include it, else print error message
if (file_exists('includes/' . $files[$page])) {
$ret = include 'includes/' . $files[$page];
} else {
$ret = "Include-File was not found: 'includes/" . $files[$page] . "'";
}
} else {
$ret = include 'includes/' . $files['notfound'];
}
}
//include header template
include 'templates/header.html';
//if the include returns an array, it contains the templatestring and the data array, try to include the template
if (is_array($ret) && isset($ret['filename'], $ret['data']) && is_string($ret['filename']) && is_array($ret['data'])) {
//if the template exists include it, else print error message
if (file_exists($file = 'templates/' . $ret['filename'])) {
$data = $ret['data'];
include $file;
} else {
$data['msg'] = 'Template "' . $file . '" was NOT found.';
include 'templates/error.html';
}
//if the include file returns a string, it returned an exception. So we print it
} else if (is_string($ret)) {
// Exception
$data['msg'] = $ret;
include 'templates/error.html';
//the defualt value of $ret didnt change, so the include didnt return anything. Print error message
} else if (1 === $ret) {
//No return array
$data['msg'] = 'No return in the template!';
include 'templates/error.html';
} else {
//include file has a complete other return like a boolean or something, print error
//everything left
$data['msg'] = 'Include file has an invalid return.';
include 'templates/error.html';
}
//include footer template
include 'templates/footer.html';
Variables.php:
<?php
$files = array();
$files['main'] = 'main.php';
$files['projects'] = 'projects.php';
$files['projects/jda-extended'] = 'projects/jda-extended.php';
$files['contact'] = 'contact.php';
$files['about'] = 'about.php';
$files['notfound'] = 'notfound.php';
一个例子包括file,projects.php:
<?php
$a = array();
$a['filename'] = 'projects.html';
$a['data'] = array();
return $a;
示例模板文件,projects.html:
<div class="fluid">
<p>
This website is still under construction.
<br><br>
<strong><a href="projects/jda-extended">JDA Extended</a></strong> - Extension to the JDA API. Allows for quick and easy discord bot development.
</p>
</div>
我相信这应该是有人可能要求的所有代码,但如果需要,可以随意提出更多要求 . 我的上一个站点与此站点之间的唯一区别是,最后一个站点是在共享Web托管上托管的 . 这个使用apache2托管在我的vps上 . 它真的让我感到困惑,因为该网站能够找到例如includes / main.php和templates / main.html,但不能找到这些文件夹中的任何其他文件 . 我唯一能想到的是htaccess文件出了问题,因为?page = projects有效,但/ project没有,但我看不出有什么问题吗?
1 回答
玩完之后,我意识到这是Apache2的一个问题 . 默认情况下
/etc/apache2/apache2.conf
AllowOverrides
设置为None
,当我需要它为All
时 . 我在Windows中完成了对网站的所有测试,我不需要费心去改变它 .