问题描述
我在遇到有关CSS和图像文件的断开链接时遇到的奇怪问题。
I am experiencing a strange problem regarding broken links to the CSS and image files.
在我们的网站上,一切正常。如果键入不存在的URL路径(例如 http://example.com/test
),则会出现未找到404的错误。
On our website everything works as expected. If you type in a non existing URL path such as http://example.com/test
the 404 not found comes up as it should.
但是,如果您输入其他路径 http://example.com/another/test.html
也会出现404页,但CSS和图像的链接已断开。
However, if you type in a different path http://example.com/another/test.html
the 404 page comes up as well but the links to the CSS and images are broken.
此外,URL偶尔会解析为 http:/ /example.com/example.com/kontakt
在其中具有实际域 example.com
。
In addition, the URL occasionally resolves to http://example.com/example.com/kontakt
having the actual domain example.com
in there.
也许有人对此问题有解释/解决方案...。
Maybe someone has an explanation/solution for this problem....
推荐答案
这是因为您正在使用资源(CSS,JS和图像文件)的相对路径。您需要使用相对于根(以斜杠开头)或绝对URL。
This is because you are using relative paths to your resources (CSS, JS and image files). You need to use either root-relative (starting with a slash) or absolute URLs.
或者,使用 base
head 部分中的>元素,它告诉浏览器相对URL相对于什么。例如:
Alternatively, use a base
element in the head
section that tells the browser what the relative URLs are relative to. For example:
<base href="http://example.com/">
(不过,请注意,使用库时有一些注意事项
标签,如果您有页内锚点,例如 href =#top
或需要支持IE6?!)
(Note, however, that there are caveats when using the base
tag if you have in-page anchors eg. href="#top"
or need to support IE6?!)
例如,类似<$ c $的URL此地址页面中的c> css / normalize.css 将解析为 http://example.com/another/css/normalize.css
,当您期望它相对于文档根目录时。
For example, a URL like css/normalize.css
in the page at this address will resolve to http://example.com/another/css/normalize.css
, when you are expecting it to be relative to the document root.
这听起来像是您的某些链接中缺少该方案,例如:
This sounds like you are missing the scheme from some of your links, for example:
<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>
另请参见我对这个问题的回答专业版网站管理员堆栈:
这篇关于找不到404-指向CSS,图像的链接断开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!