douhuireng4407 2018-09-30 23:20
浏览 81

只找到主页面

So I just started making a new website, or rather modifying an old one. All the code is the same, the only difference is I changed some text here and there. The old website worked perfectly, so this one should as well, so I'm not sure why this is happening, but only the main page can be found using the URLs that I wish to be used. Try going to, for example, the projects page, and not only is the file not found, but my custom not found page won't even be shown. First I'll show some examples then I will show some code.

Here is my htaccess file:

//deny access to this file
<Files ~ ".htaccess">
deny from all
</Files>

//start RewriteEngine
RewriteEngine On

//if the called file is NOT a directory, file or link, we call index.php?page=
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ index.php?page=$1 [QSA,L]

//disable file listing
Options -Indexes

//set error page 403 (no permission) and 404 (page not found) to our notfound page
ErrorDocument 403 /notfound
ErrorDocument 404 /notfound
order deny,allow

(# replaced with //)

My 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';

An example include file, projects.php:

<?php

$a = array();
$a['filename'] = 'projects.html';
$a['data'] = array();

return $a;

Example template file, 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>

I believe that should be all the code someone might ask for, but feel free to ask for more if need be. The only difference between my last site and this one, is that the last site was hosted on shared web hosting. This one is hosted on my vps using apache2. It really confuses me as the site is able to find for example includes/main.php and templates/main.html, but not any other files in those folders. The only thing I can think of is something went wrong in the htaccess file as ?page=projects works but /projects does not, but I can't see anything wrong with it?

  • 写回答

1条回答 默认 最新

  • dptgpyl61857413 2018-10-01 03:27
    关注

    After playing around, I realized it was an issue with Apache2. By default in /etc/apache2/apache2.conf AllowOverrides is set to None when I needed it to be All. I had done all my testing of the website in windows, which I did not need to bother changing that.

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题