所以我才刚开始创建一个新网站,或者只是修改一个旧网站。所有代码都是相同的,唯一的区别是我在这里和那里更改了一些文本。旧的网站运行良好,因此也应该如此,所以我不确定为什么会这样,但是使用我希望使用的URL只能找到主页。例如,尝试转到项目页面,不仅找不到文件,而且甚至不会显示我的自定义未找到页面。首先,我将显示一些示例,然后将显示一些代码。
这是我的htaccess文件:
///拒绝访问此文件
<文件〜“ .htaccess”>
全部拒绝
//启动RewriteEngine
RewriteEngine开启///如果调用的文件不是目录,文件或链接,则我们称为index.php?page =
RewriteCond%{REQUEST_FILENAME}!-d
RewriteCond%{REQUEST_FILENAME}!-f
RewriteCond%{REQUEST_FILENAME}!-l
RewriteRule ^(。*)$ index.php?page = $ 1 [QSA,L]
//禁用文件列表
选项-索引//将错误页面403(无权限)和404(找不到页面)设置为我们未找到的页面
ErrorDocument 403 /未找到
ErrorDocument 404 / notfound
命令拒绝,允许
(#替换为//)
我的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';
示例包括文件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>
我认为这应该是某人可能要求的所有代码,但是如果需要,可以随时要求更多。我的上一个站点与该站点之间的唯一区别是,上一个站点托管在共享的虚拟主机上。这是使用apache2托管在我的vps上的。这真的使我感到困惑,因为该站点能够找到例如include / main.php和templates / main.html,但找不到这些文件夹中的任何其他文件。我唯一能想到的是htaccess文件中发生了什么问题,因为?page = projects有效,但/ projects却没有,但是我看不到有什么问题吗?
答案 0 :(得分:0)
玩转之后,我意识到这与Apache2有关。我需要将/etc/apache2/apache2.conf
中的AllowOverrides
中的None
默认设置为All
时。我已经在Windows中对网站进行了所有测试,而无需费心更改它。