Welcome to WebmasterWorld Guest from 54.157.222.62

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

RewriteCond $1

   
8:30 am on Jun 24, 2011 (gmt 0)

5+ Year Member



Hi.

I'm looking for information on what "RewriteCond $1 ..." means. In the docs I read that $1...$N are backreferences, but sometimes I find this combination to be used as first statement also, something like this:

RewriteEngine on
RewriteCond $1 \.(gif|jpg|ico|css|js)$ [NC,OR]

So it's not clear to me what the backreference references to.

I guess it's should be the same as %{REQUEST_URI}, but I'm looking for a confirmation here.

Thank you.
10:12 am on Jun 24, 2011 (gmt 0)

10+ Year Member



$1 is the backreference to the regular expression bracket set on the right side. So, to break it up:

RewriteCond $1 \.(gif|jpg|ico|css|js)$ [NC,OR]


$1 would reference a matched file extension.

If you had more bracket sets in the regular expressions, you would have access to $1 and $2 and so on depending on the number of bracket combinations.
10:50 am on Jun 24, 2011 (gmt 0)

5+ Year Member



And to which string the RewriteCond is applying the reg exp \.(gif|jpg|ico|css|js)$ ?
4:55 pm on Jun 24, 2011 (gmt 0)

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



$1 is the backreference to the regular expression bracket set on the right side.

Assuming for the sake of discussion that your .htaccess is written in a right-to-left script. You can back-reference a previous capture, but you can't back-reference something that doesn't exist yet.

[httpd.apache.org...]
and
[httpd.apache.org...]

But you've got that page bookmarked already ;)
10:32 pm on Jun 24, 2011 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



RewriteCond $1 !\.gif$
RewriteRule ^folder/(.*) http://www.example.com/$1 [R=301,L]


It might help to know that the RewriteCond is NOT processed first.

Processing order is:
- evaluate
^folder/(.*)
pattern to see if "true",
- if true, the (.*) "creates" the $1 backreference (which essentially holds the requested "filename"),
- evaluate the RewriteCond; which checks if the requested "filename" does not end with ".gif",
- redirect to a new URL, one without "folder/" in it; the browser will have to make a new request.
8:39 pm on Jun 25, 2011 (gmt 0)

5+ Year Member



Thanks, I got it now, it looks like more a forward reference rather than a back reference, in this case :v
9:47 pm on Jun 25, 2011 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



Just remember that the RewriteRule RegEx pattern "creates" the $n references and the RewriteCond RegEx patterns "create" the %n references.

The numbering is ascertained by counting the opening parentheses, the first being $1 or %1.