【问题标题】:Redirect using htaccess for 404 errors使用 htaccess 重定向 404 错误
【发布时间】:2015-03-24 00:34:23
【问题描述】:

我的 htaccess 文件中有以下内容用于 404 重定向

ErrorDocument 404     /index.html

如您所见,如果有人点击了一个不存在的 URL,例如

http://example.com/NonExistingFolder/nonExisting.html

虽然这确实重定向到我位于根级别的 index.html, 无法定位 index.html 中引用的 css、js 文件 因为它试图在 NonExistingFolder

下找到那些

我该如何解决这个问题?

【问题讨论】:

    标签: .htaccess redirect http-status-code-404 cpanel


    【解决方案1】:

    这是正常行为,因为您肯定对所有资源(图像、css、js、href 链接等)使用相对 路径。

    要解决这个“问题”,您必须改用 absolute 路径。

    一种方法是用绝对链接替换所有相对链接。
    示例(用于 css):

    <link rel="stylesheet" type="text/css" href="css/style.css">
    

    变成

    <link rel="stylesheet" type="text/css" href="/css/style.css">
    

    注意前面的斜杠,表示path is starting at root level

    另一种解决方案是在所有页面中的&lt;head&gt; html 标记之后添加&lt;base href="/"&gt;

    【讨论】:

      【解决方案2】:

      如果你想明确禁用路径信息,那么你必须在你的 .htaccess 文件中使用:

      AcceptPathInfo Off
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-07-07
        • 1970-01-01
        • 2013-11-26
        • 2013-06-09
        • 2018-03-20
        • 2023-03-10
        • 2015-03-20
        • 2014-03-20
        相关资源
        最近更新 更多