homepage Welcome to WebmasterWorld Guest from 23.20.28.193
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
RewriteCond $1
Grimmjow




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

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.

 

alias




msg:4330450
 10:12 am on Jun 24, 2011 (gmt 0)

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

Grimmjow




msg:4330467
 10:50 am on Jun 24, 2011 (gmt 0)

And to which string the RewriteCond is applying the reg exp \.(gif|jpg|ico|css|js)$ ?

lucy24




msg:4330635
 4:55 pm on Jun 24, 2011 (gmt 0)

$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 ;)

g1smd




msg:4330796
 10:32 pm on Jun 24, 2011 (gmt 0)

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.

Grimmjow




msg:4331069
 8:39 pm on Jun 25, 2011 (gmt 0)

Thanks, I got it now, it looks like more a forward reference rather than a back reference, in this case :v

g1smd




msg:4331087
 9:47 pm on Jun 25, 2011 (gmt 0)

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.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved