【问题标题】:Removing .html on paths with htaccess使用 htaccess 删除路径上的 .html
【发布时间】:2015-08-01 14:45:26
【问题描述】:

我想删除我所有链接末尾的.html,例如www.mywebsite.com/contact.html应该是www.mywebsite.com/contact

下面的代码完成了这项工作:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]

我还希望我的目录文件也删除.html,即www.mywebsite.com/rates/index.html 应该是www.mywebsite.com/rates

它正在使用以下代码:

RewriteCond %{REQUEST_URI} /index\.html?$ [NC]
RewriteRule ^(.*)index\.html?$ "/$1" [NC,R=301,NE,L]

但是在一起,它们不起作用。如果第二个位于第一个下方,则只有第一个起作用。如果我只有第二个选项,我的第一个选项不起作用并且我的所有链接都已损坏。如何将两者结合起来?

【问题讨论】:

    标签: html .htaccess mod-rewrite


    【解决方案1】:

    添加您的.htaccess 文件

    Options +FollowSymlinks -MultiViews
        RewriteEngine on
    
        # to make `/path/index.html` to /path/
        RewriteCond %{THE_REQUEST} ^GET\s(.*/)index\.html [NC]
        RewriteRule . %1 [NE,R=301,L]
    
        RewriteCond %{THE_REQUEST} ^GET\s.+\.html [NC]
        RewriteRule ^(.+)\.html$ /$1 [NE,R=301,L,NC]
    
        RewriteCond %{REQUEST_URI} !\.html$ [NC]
        RewriteCond %{REQUEST_FILENAME}.html -f
        RewriteRule . %{REQUEST_URI}.html [L]
    

    【讨论】:

    • 谢谢!那行得通。实际上比我想象的要复杂得多。我还不能将答案标记为正确(必须等待 8 分钟),但我会尽快标记。
    猜你喜欢
    • 1970-01-01
    • 2012-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多