【问题标题】:direct all webpage requests with .index.html to /将所有带有 .index.html 的网页请求定向到 /
【发布时间】:2011-05-24 17:18:42
【问题描述】:

我想将所有以 index.html 结尾的 URL 的请求定向到 /。我在服务器上有一个域。

例子:

  • 如果有人想要“www.thissite.com/index.html--它被定向到 www.thissite.com/。 和
  • 如果有人想要“www.thissite.com/anyword/index.html”——它会被定向到 www.thissite.com/。 和
  • 如果有人想要“www.thissite.com/folderdoesntexistonthissite/index.html”--它会被定向到 www.thissite.com/。

启用此功能的 .htaccess 代码是什么? (重写条件和重写规则)

这并不能很好地完成工作:

RewriteCond %{THE_REQUEST} index\.html [NC]
RewriteRule index\.html$ http://www.thissite.com/$1 [R=301.L]

【问题讨论】:

    标签: apache .htaccess mod-rewrite url-rewriting


    【解决方案1】:

    你可以试试这个(没有 RewriteCond):

    RewriteRule /index\.html$ http://www.thissite.com/ [R=301,NC,L]
    

    可能错误是[R=301.L] 中的句点。

    【讨论】:

      【解决方案2】:

      您需要在 RewriteCond 中使用 %{REQUEST_URI} 变量来匹配,否则 Apache 会在 RewriteRule 中删除从 / 开始的内容。在您的 .htaccess 文件中使用以下代码:

      Options +FollowSymlinks -MultiViews
      RewriteEngine on
      
      RewriteCond %{REQUEST_URI} ^.*/index.html$ [NC]
      RewriteRule . / [R=301,L]
      

      【讨论】:

        猜你喜欢
        • 2013-02-15
        • 1970-01-01
        • 2016-05-04
        • 1970-01-01
        • 1970-01-01
        • 2017-05-29
        • 2023-04-09
        • 2016-10-18
        • 2020-09-30
        相关资源
        最近更新 更多