Difference Between $1 vs %1 In. Htaccess

Difference Between $1 vs %1 In. Htaccess

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

For example,

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

    RewriteRule .*  [R=301,L]    
    #    versus
    #    RewriteRule .*  [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.

1

1 Answer

%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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

David Miller
Author

David Miller

David Miller brings 15 years of experience in global economics, personal finance strategy, and market dynamics. He specializes in turning complex economic trends into actionable insights for everyday readers.