【问题标题】:htaccess problem with filenamehtaccess 文件名问题
【发布时间】:2009-07-30 15:39:59
【问题描述】:

当我第一次设置我的网站时,我决定将index.html 添加到 URL,但现在当人们删除 index.html 并尝试访问该文件夹时我遇到了问题...

例如:

RewriteRule ^archives/([0-9]+)/([0-9]+)/index.html archive.php?mid=$1-$2

那么当archives/07/2009/ 时会报错,我该如何避免这个报错呢? 干杯

【问题讨论】:

    标签: .htaccess mod-rewrite


    【解决方案1】:

    试试这个规则,加上可选的index.html

    RewriteRule ^archives/([0-9]+)/([0-9]+)/(index\.html)?$ archive.php?mid=$1-$2
    

    但我建议您只使用这两种表示法中的一种,带有或不带有尾随 index.html 并在错误时重定向:

    # remove index.html
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*/)?index\.html$ /$1 [L,R=301]
    
    # add index.html
    RewriteRule (.*)/$ $1/index.html [L,R=301]
    

    【讨论】:

    • @Gumbo: 您可能希望将index\.html 设为非捕获组。
    • @Andrew Moore:非捕获组仅从 Apache 2 开始可用。
    【解决方案2】:

    在您的RewriteRule 中将index.html 设为可选:

    RewriteRule ^archives/([0-9]+)/([0-9]+)/(?:index\.html)?$ archive.php?mid=$1-$2
    

    另外,在您原来的重写规则中,您忘记了字符串锚点的结尾$。我已经在上面添加了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多