.htaccess - 404 未找到 - CSS、图像的链接已损坏

标签 .htaccess http-status-code-404 broken-links broken-image

我遇到了一个关于 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 domain example.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/

相关文章:

.htaccess - 使用 .htaccess 强制 www 和 https

c# - 如何使用 ASP.NET 为文件夹设置特定的 404?

ruby-on-rails - Rails 4 自定义 404 导致 Heroku 上的 postgresql 连接失败

seo - Broken Link 测试 404 错误,但页面出现在浏览器中

image - 为什么 magento 中的 wysiwyg 插件让我断开链接?

php - 将特殊 PHP 文件重定向到子域

Apache 307 Redirect 重定向 POST 数据

php - 如何使用 .htaccess 隐藏我的 URL 参数

asp.net - 当请求太大时,为什么 IIS 返回代码 404.13 而不是代码 413?

html - 链接不返回目录?