【问题标题】:.htaccss Rewrite Rules.htaccess 重写规则
【发布时间】:2026-01-10 21:00:01
【问题描述】:

我目前有一个文件夹结构

root/page1.html

root/page2.html

root/page3.html

root/page3/subpage1.html

并尝试使用重写规则在浏览器中显示时隐藏 .html 部分。

目前 .htaccess 看起来像:

#Alternate default index page
DirectoryIndex home.html

RewriteCond %{DOCUMENT_ROOT}/$1.html -f
RewriteRule ^(.+?)/?$ $1.html [L]

我遇到的问题是,当我尝试单击一个链接时,该链接会将我带到 root/page3.html 它的 404ing,因为它试图重定向到 root/page3/.html

解决此问题的最佳方法是什么?我应该为此使用 .htaccess 还是更改我的结构?

【问题讨论】:

    标签: html .htaccess directory-structure


    【解决方案1】:

    把你的规则改成这样:

    RewriteEngine On
    
    RewriteCond %{REQUEST_FILENAME}.html -f
    RewriteRule ^(.+?)/?$ $1.html [L]
    

    【讨论】:

    • 所以它现在试图访问 root/page3/ 作为一个看起来像的目录,而不是 html 页面本身。
    • 看起来像一个目录网页,我已经编辑了我的原始帖子以包含完整的 .htaccess 代码
    • 您在浏览器中看到的 URL 是什么,究竟是什么错误?
    • 使用当前的 .htaccess 我的 url 正在读取 mywebsite.com/artists/ 它没有出错,因为它仍然是一个包含多个 .html 页面的目录,但我希望完成的是它是 mywebsite.com/artists.html,同时隐藏 URL 的 .html 部分。
    • 您已经给出了page1page2 等问题的示例,但没有提到您的html 文件名与目录名相同。首先使用站点根目录中存在的有效.html 文件测试此规则,看看它是否有效。如果这也不起作用,请使用完整的 ,htaccess 编辑您的问题