【问题标题】:apache2 adding new websiteapache2添加新网站
【发布时间】:2016-11-10 10:25:18
【问题描述】:

我对 apache 还是很陌生。我想创建一个由 apache 托管的网站,所以这就是我所做的: 我在 /var/www/html 中创建了一个名为“test”的新文件夹 我将 index.html、js 和 css 文件放在新文件夹中

然后我尝试通过localhost/test访问网站,显示html页面,但是无法访问js和css文件。在控制台中显示“NetworkError:404 Not Found - http://localhost/main.js” 为什么 url 中缺少“/test”?

我的 index.html 有 <script type="text/javascript" src="main.js"> 谁能弄清楚是什么问题?

更新 我检查了 dir.conf 并添加了“DirectorySlash on”,现在它看起来像: <IfModule mod_dir.c> DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm DirectorySlash on </IfModule> 我注意到当我尝试访问 http://localhost/test 时,它会在末尾添加“/”,但我仍然遇到同样的错误。

【问题讨论】:

  • “我注意到当我尝试访问 localhost/test 时,它会在末尾添加 '/',但我仍然遇到同样的错误。” — 在其他条件相同的情况下:这不会发生。您需要提供一个minimal reproducible example,其中包含足够的信息来重现问题。

标签: html apache apache2


【解决方案1】:

当您使用带有相对路径的 URL 时,它是相对于它相对的 URL 中的最后一个 / 计算的。

由于您正在访问:

http://localhost/test

test 被修剪,main.js 被添加。

如果你在

http://localhost/test/

那么最后一个/ 在最后,所以它会解析为http://localhost/test/main.js


如果test 是一个目录,Apache 通常会将你从/test 重定向到/test/(特别是为了避免这个问题)。你应该编辑你的配置文件来启用the directoryslash directive:

DirectorySlash on

这是默认设置,但您必须有一个禁用它的版本。

【讨论】:

  • 我按照你的建议做了,但我遇到了同样的错误。我更新了我所做的。我从命令“sudo apt-get install apache2”安装了 apache2,但我什么也没做。通常都是默认的。
猜你喜欢
  • 2016-12-01
  • 2020-01-03
  • 1970-01-01
  • 1970-01-01
  • 2014-05-04
  • 1970-01-01
  • 2013-02-14
  • 2020-03-10
  • 2017-07-22
相关资源
最近更新 更多