【问题标题】:Difference between $1 vs %1 in .htaccess.htaccess 中 $1 与 %1 之间的差异
【发布时间】:2011-10-03 01:07:41
【问题描述】:

.htaccess 中的%1 和$1 有什么区别?

例如,

    #  to remove www  
    RewriteCond %{HTTP_HOST} ^(\w+)\.mydomain\.com [NC]

    RewriteRule .* http://mydomain.com/%1 [R=301,L]    
    #    versus
    #    RewriteRule .* http://mydomain.com/$1 [R=301,L]

我一直在使用 Dave Child's .htaccess cheat sheet 和 Jackol's .htaccess cheat sheet 以及 Apache mod_rewrite docs,但额外的帮助会很棒。

【问题讨论】:

  • mod_rewrite 文档说 %N 反向引用用于 RewriteCond 模式,而 $N 反向引用用于 RewriteRule 模式。

标签: apache .htaccess mod-rewrite


【解决方案1】:

%1 指的是在RewriteCond 条件中匹配的模式,而$1 指的是在RewriteRule 中匹配的模式。

更一般地,使用%n 来引用来自RewriteCond 条件正则表达式模式的编号匹配,并使用$n 来引用来自RewriteRule 正则表达式模式的编号匹配。

【讨论】:

    猜你喜欢
    • 2017-03-02
    • 2021-03-19
    • 1970-01-01
    • 1970-01-01
    • 2019-03-28
    • 2014-07-22
    • 1970-01-01
    • 2022-09-23
    • 1970-01-01
    相关资源
    最近更新 更多