【问题标题】:.htaccess with or without slash.htaccess 带或不带斜线
【发布时间】:2010-10-07 03:08:42
【问题描述】:

我需要对以下重写规则执行什么操作才能使其正常工作,无论它们是否是 URL 末尾的斜线?

即。 http://mydomain.com/content/featured 要么 http://mydomain.com/content/featured/

RewriteRule ^content/featured/ /content/today.html 

【问题讨论】:

    标签: .htaccess mod-rewrite rewrite


    【解决方案1】:

    简单的方法:

    RewriteEngine On
    RewriteBase / 
    RewriteRule ^content/featured(\/||)$ /content/today.html [L,R=301,NC] 
    

    【讨论】:

      【解决方案2】:

      使用$ 标记字符串的结尾,使用? 标记前面的表达式重复零次或一次:

      RewriteRule ^content/featured/?$ content/today.html
      

      但我建议您坚持一种符号并纠正拼写错误:

      # remove trailing slashes
      RewriteRule (.*)/$ $1 [L,R=301]
      
      # add trailing slashes
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteRule .*[^/]$ $0/ [L,R=301]
      

      【讨论】:

      • +1 主要是为了提供更多信息,尽管我还是想把它给你。
      • @JensTörnell $0 包含整个匹配的字符串。
      猜你喜欢
      • 2021-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-26
      • 2013-03-29
      • 1970-01-01
      • 2014-09-16
      • 1970-01-01
      相关资源
      最近更新 更多