我遇到了一个关于 CSS 和图像文件链接损坏的奇怪问题。
在我们的网站上,一切都按预期进行。如果您输入不存在的 URL 路径(例如 http://example.com/test
),则会出现 404 not find 错误。
但是,如果您输入不同的路径http://example.com/another/test.html
,也会出现 404 页面,但指向 CSS 和图像的链接会损坏。
此外,URL 有时会解析为 http://example.com/example.com/kontakt
,其中包含实际域 example.com
。
也许有人对这个问题有解释/解决方案......
最佳答案
这是因为您使用的是资源(CSS、JS 和图像文件)的相对路径。您需要使用根相对 URL(以斜线开头)或绝对 URL。
或者,在 head
部分使用 base
元素来告诉浏览器相对 URL 的相对位置。例如:
<base href="http://example.com/">
(但是请注意,如果您有页内 anchor ,例如 href="#top"
或需要支持,则使用 base
标记时需要注意IE6?!)
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.
例如,此地址页面中类似 css/normalize.css
的 URL 将解析为 http://example.com/another/css/normalize.css
code>,当您期望它相对于文档根目录时。
In addition, the URL occasionally resolves to
http://example.com/example.com/kontakt
having the actual domainexample.com
in there.
这听起来像是您缺少某些链接中的方案,例如:
<a href="example.com/kontakt">Link Text</a>
其实应该是:
<a href="http://example.com/kontakt">Link Text</a>
或者,相对协议(protocol):
<a href="//example.com/kontakt">Link Text</a>
另请参阅我在专业网站管理员堆栈上对此问题的回答: https://webmasters.stackexchange.com/questions/86450/htaccess-rewrite-url-leads-to-missing-css
关于.htaccess - 404 未找到 - CSS、图像的链接已损坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43737411/