2

我遇到了一个关于 CSS 和图像文件的链接断开的奇怪问题。

在我们的网站上,一切都按预期工作。如果您输入一个不存在的 URL 路径,例如http://example.com/test404 not found,它应该会出现。

但是,如果您输入不同的路径http://example.com/another/test.html,也会出现 404 页面,但指向 CSS 和图像的链接会损坏。

此外,该 URL 有时会解析为http://example.com/example.com/kontakt其中包含实际域example.com

也许有人对此问题有解释/解决方案....

4

2 回答 2

3

这是因为您使用的是资源(CSS、JS 和图像文件)的相对路径。您需要使用相对根(以斜杠开头)或绝对 URL。

或者,使用部分base中的元素head告诉浏览器相对 URL 是相对于什么。例如:

<base href="http://example.com/">

(但请注意,base如果您有页内锚点,href="#top"或者需要支持 IE6,则在使用标签时需要注意事项?!)

但是,如果您输入不同的路径http://example.com/another/test.html,也会出现 404 页面,但指向 css 和图像的链接会损坏。

例如,当您期望它与文档根目录相关时css/normalize.css,此地址的页面中的 URL 将解析为。http://example.com/another/css/normalize.css

此外,该 URL 有时会解析为http://example.com/example.com/kontakt其中包含实际域example.com

这听起来像您缺少某些链接中的方案,例如:

<a href="example.com/kontakt">Link Text</a>

而它应该是:

<a href="http://example.com/kontakt">Link Text</a>

或者,协议相关

<a href="//example.com/kontakt">Link Text</a>

另请参阅我在 Pro Webmasters 堆栈上对这个问题的回答: https ://webmasters.stackexchange.com/questions/86450/htaccess-rewrite-url-leads-to-missing-css

于 2017-05-02T12:11:46.083 回答
0

就我而言,locationconf的文件中已经存在链接损坏的文件的块,只是没有正确配置这些块。

这就是我在我的default.conf. 对于所有以“/sad.svg”结尾的 URI,它会为指定目录中的文件提供服务,而忽略 URI 中前面的任何路径。

...
  error_page 404 /404.html;
  location = /404.html {
          root /var/www/errors;
          internal;
  }

  location ~* ".*/sad.svg"  {
      alias /var/www/errors/sad.svg;
      access_log off;
  }
  location ~* ".*/twitter.svg" {
      alias /var/www/errors/twitter.svg;
      access_log off;
  }
  location ~* ".*/gitlab.svg" {
      alias /var/www/errors/gitlab.svg;
      access_log off;
  }
...
于 2019-06-15T01:10:07.713 回答