【问题标题】:.htaccess DirectoryIndex page links broken.htaccess DirectoryIndex 页面链接损坏
【发布时间】:2013-10-27 15:26:15
【问题描述】:

在我的 .htaccess 文件中,DirectoryIndex 设置为 /page/main/home.html

因此,当我转到www.thisexample.com 时,浏览器地址栏显示www.thisexample.com,而实际加载www.thisexample.com/page/main/home.html。这一切都很好,但现在问题来了:

无法找到从home.html 链接到的任何.html 页面。换句话说,当我点击信息链接(在主页上)时,它会寻找www.thisexample.com/info.html(不存在)而不是在浏览器地址栏中显示www.thisexample.com/info.html,而是实际加载www.thisexample.com/page/main/info.html(这里的最终目标) .

添加 home.html 通过相对路径找到它的 css 样式表没有问题可能会有所帮助,只是找不到链接。

【问题讨论】:

    标签: .htaccess directoryindex


    【解决方案1】:

    DirectoryIndex 应该是默认 文件 的名称,例如 index.htmlindex.php,例如:

    DirectoryIndex index.html
    

    如果你想从/page/main 加载所有.html 文件,那么有这些重写规则:

    DirectoryIndex home.html
    
    RewriteEngine On
    
    RewriteCond %{REQUEST_URI} !\.(?:jpe?g|gif|bmp|png|tiff|css|js)$ [NC]
    RewriteRule !^page/main/ page/main%{REQUEST_URI} [L,NC]
    

    还要开始在你的 css、js、图像文件中使用绝对路径而不是相对路径。这意味着您必须确保这些文件的路径以 http:// 或斜杠 / 开头。

    【讨论】:

    • 我在/public_html.htaccess 文件中添加了RewriteRule !^page/main/ /page/main%{REQUEST_URI} [L]。我那里还有DirectoryIndex /page/main/home.html,否则它只会把我带到父目录。链接仍然无效。你能澄清一下实现吗?
    • 如果您发现 css、js、图像链接损坏,请尝试在 HTML 页面顶部使用 <base href="/" />
    • 你只需要DirectoryIndex home.html
    • .htaccess 现在有 RewriteRule !^page/main/ /page/main%{REQUEST_URI} [L]DirectoryIndex home.html 。不幸的是,现在它只是把我带到了父目录。 css 和 js 没问题。它们链接为"/../../css/stylesheet.css"。那里没问题。关于链接的任何想法?
    • 参见上面的编辑代码。具体来说,您需要顶部的 RewriteEngine On 行。