1

当用户输入任何不存在的 url 并将请求转发到错误控制器时,站点运行良好。但

  • 如果您在 url 站点中编写脚本,则抛出 403 错误
  • 如果你写一些 asp 代码网站会抛出 400 错误

我怎样才能防止这种情况并将它们转发到自定义 403 和 404 页面?我尝试使用 htaccess,但我无法成功。

另一个问题:

  • 如果您在地址栏中编写任何 ascii 代码,引导前进到欢迎页面(控制器 - > index.php)。怎么可能?

目录结构、htaccess 和引导代码如下。感谢您的任何帮助。

目录结构:

/config
/libs
   -bootstrap.php
/controllers
/models
/views
/public_html
  -index.php
   htaccess
htaccess

htaccess 在主目录中

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule    ^$    public_html/    [L]
RewriteRule    (.*) public_html/$1    [L]
</IfModule>

public_html 目录中的 htaccess

Options -Indexes

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1 [PT,L]
</IfModule>

public_html 中的 index.php

<?php

define('DS', DIRECTORY_SEPARATOR);
define('ROOT', dirname(dirname(__FILE__)));
require_once (ROOT . DS . 'libs' . DS . 'bootstrap.php');

$app = new Bootstrap();

库中的 bootstrap.php

<?php

class Bootstrap {

    function __construct() {

        $url = isset($_GET['url']) ? $_GET['url'] : null;
        $url = rtrim($url, '/');
        $url = explode('/', $url);

        print_r($url);

        if (empty($url[0])) {
            require '../controllers/index.php';
            $controller = new Index();
            $controller->index();
            return false;
        }

        $file = '../controllers/' . $url[0] . '.php';
        if (file_exists($file)) {
            require $file;
        } else {
            $this->error();
            return false;
        }

        $controller = new $url[0];

        // calling methods
        if (isset($url[2])) {
            if (method_exists($controller, $url[1])) {
                $controller->{$url[1]}($url[2]);
            } else {
                $this->error();
            }
        } else {
            if (isset($url[1])) {
                if (method_exists($controller, $url[1])) {
                    $controller->{$url[1]}();
                } else {
                    $this->error();
                }
            } else {
                $controller->index();
            }
        }


    }

    function error() {
        require '../controllers/error.php';
        $controller = new Error();
        $controller->index();
        return false;
    }

}
4

0 回答 0