43

What is the difference between %1 and $1 in .htaccess?

For example,

    #  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]

I have been using Dave Child's .htaccess cheat sheet and Jackol's .htaccess cheat sheet as well as the Apache mod_rewrite docs but additional help would be great.

random
  • 9,774
  • 10
  • 66
  • 83
csi
  • 9,018
  • 8
  • 61
  • 81
  • 6
    The `mod_rewrite` docs say `%N` backreferences are for RewriteCond patterns while `$N` backreferences are meant for RewriteRule patterns. – BoltClock Jul 11 '11 at 18:40

1 Answers1

72

%1 Refers to a pattern matched in a RewriteCond condition, while $1 refers to a pattern matched inside a RewriteRule.

More generically, use %n to refer to the numbered matches from RewriteCond condition regex patterns, and use $n to refer to numbered matches from RewriteRule regex patterns.

Michael Berkowski
  • 267,341
  • 46
  • 444
  • 390