Percent Number in Apache Rewrite Rules (mod_rewrite)

What do the %1 %2 in a Rewrite rule mean? The Apache guide does not help, nor does any other documentation I found. I came across the %1, %2 etc in some complex and arcane rules. Google ignores percent signs, which makes it hard to get an easy answer.

We'll use the rules from my last post as an example.

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([a-z.]+)$ [NC]
RewriteRule ^/(.*)$ http://www.%1/$1 [R=301,L]

The %1 refers to the capture group in a previous RewriteCond. This differentiates it from the $1 which refers to a capture group in the current RewriteRule.

Hopefully I have littered this post with enough keywords that future googlers will find the answer to the %1 %2 in ReWrite rules more easily 🙂



6 responses to “Percent Number in Apache Rewrite Rules (mod_rewrite)”

5 11 2009
Rob Hunter (17:23:27) :

To further help the keywords and the lost and lonely Web searcher, those numbers are called backreferences. Their use in mod_rewrite are described in the Apache manual.

5 11 2009
Rob Hunter (17:25:07) :

Ack, that <code> block was meant to be an inline element. Any chance of a "preview comment" feature?

5 11 2009
James (22:34:54) :

Thanks Rob, that Apache mod_rewrite manual reference is handy.

17 02 2010
Brian Egge (14:45:16) :

What makes it confusing is you 1) you can reference two different sets of backreferences, and 2) every regex implementation uses a different syntax for backreferences. It's little wonder there's over 1000 questions tagged mod_rewrite on

5 05 2010
loconet (01:48:15) :

Thank you very much for taking the time to write this. Specially saying spelling it out to get caught by google.

11 05 2010
fdeel (20:23:51) :

This code I get from the webhosting maintenance.

RewriteCond %{HTTP_HOST} ^(.*)$ [NC]
RewriteRule (.*) [R=301,L]

How do I read this code in my program with and wat is the read or write
with kind regards.