我有一个具有此目录结构的简单 php Web 应用程序
.
├── custom_403.html
├── custom_404.html
├── home
│ └── home.php
├── .htaccess
├── index.php
├── login
│ └── login.php
├── public
│ ├── bootstrap
│ ├── css
│ ├── error_pages
│ ├── fontawesome
│ ├── .htaccess
│ └── js
├── signup
│ └── signup.php
├── test.php
我制作了一个.htacess基本上看起来像这样的文件
ErrorDocument 404 /public/error_pages/404.php
ErrorDocument 500 /public/error_pages/500.php
ErrorDocument 403 /index.php
当我键入类似的http://localhost/login/内容时,它应该给我一个403 Forbidden错误,因为目录login存在但没有文件index.php。根据我.htacess的说法,它应该提供默认文件。我index.php利用这个在index.php使用 jquery 从地址栏加载指定路径中的文件。我搜索window.location.pathname使用函数从该路径加载相应的文件resolveUrl()。
这是一个例子
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<!-- Boostrap 4.3 -->
<link rel="stylesheet" href="/public/bootstrap/css/bootstrap.css">
<!-- Fontawesome 5 Free -->
<link rel="stylesheet" href="/public/fontawesome/css/all.css">
<link rel="stylesheet" href="/public/css/style.css">
<script src="/public/js/jquery.js"></script>
<script src="/public/bootstrap/js/bootstrap.js"></script>
</head>
<body>
<nav class="navbar navbar-expand-lg r-shadow">
<a class="navbar-brand" href="#"><img width="50" class="d-inline-block align-top" src="https://seeklogo.com/images/M/marvel-comics-logo-31D9B4C7FB-seeklogo.com.png" alt="" srcset=""></a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarText" aria-controls="navbarText" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse justify-content-end" id="navbarText">
<ul class="navbar-nav">
<li class="nav-item active">
<a class="nav-link" href="home">Home</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="login">Login</a>
</li>
<li class="nav-item">
<a class="nav-link" href="signup">Sign Up</a>
</li>
</ul>
</nav>
<div class="root">
<div class="container mt-3 rounded" style="background-color: rgba(255, 255, 255, 0.1); ">dcfdcf</div>
<div id="loader">
<div class="spinner"><i class="fas fa-spinner fa-spin"></i></div>
</div>
</div>
</body>
<script>
function resolveUrl(url) {
if (url) {
var path = url.replace(/\//gi, '').trim();
$(".container").load("/" + path + "/" + path + ".php");
return path;
} else {
$(".container").load("/home/home.php");
console.log(path)
}
}
function resolveActiveTab(pathName) {
$(".nav-item").removeClass("active");
if (pathName) {
$("li > a[href='" + pathName + "']").parent().addClass("active");
} else {
$("li > a").first().parent().addClass("active");
}
}
$(document).ready(function () {
console.log(window.location.pathname)
var currentPath = resolveUrl(window.location.pathname);
resolveActiveTab(currentPath)
$(document).ajaxStart(function () {
$("#loader").css({
"display": "block"
});
})
$(document).ajaxStop(function () {
$("#loader").css({
"display": "none"
});
})
$('.nav-link').click(function (e) {
e.preventDefault();
var path = $(this).attr("href");
$(".container").load("/" + path + "/" + path + ".php");
resolveActiveTab(path)
window.history.pushState("", "", "/" + path);
})
})
</script>
</html>
这意味着如果刷新页面时http://localhost/login/会加载类似 jquery的路径login.php。
这工作正常,除了我在 apache 日志上得到一个看起来像这样的错误
AH01276:无法提供目录 /var/www/html/test/login/:找不到匹配的 DirectoryIndex (index.php,index.html,index.cgi,index.pl,index.xhtml,index.htm),并且服务器- Options 指令禁止生成的目录索引
当我尝试通过将文件名从 更改为 来解决此问题时login.php signup.php etc,index.php我会返回这些文件,因为它们确实存在,这意味着我不会收到403错误消息。我尝试拒绝访问我的这些特定文件,.htaccess但随后它们被拒绝访问包括 jquery.load()函数在内的所有内容。我的问题是如何在没有 apache 错误的情况下有效地做我正在做的事情。