back references

ISAPI_Rewrite is Apache mod_rewrite compatible URL rewriter for Microsoft IIS
User avatar
Posts: 3
Joined: 10 Apr 2013, 12:11

back references

10 Apr 2013, 15:20

Hi,

I am trying to reverse engineer some code and need some help understanding back references. from the documentation I understand that back references are to RewriteCond statements when used within a Rewrite rule. Now based on the below code:

RewriteCond %{REQUEST_URI} !^/favicon.ico [NC]
RewriteCond %{REQUEST_URI} !^/SomeHeader/ [NC]
RewriteCond %{HTTP_COOKIE} ^((?!lastview).)*$ [NC]
RewriteCond %{HTTP:Host} (.*)
RewriteCond %{REQUEST_URI} (.*)
RewriteCond %{QUERY_STRING} (.*)

RewriteRule .? https\://%2/SomeHeader/default\.aspx\?requesturl=https\://%2%3\?%4 [R=301]

what will the values be inside %1,%2,%,3 and %4 and is there any more %N where N > 4. The reason I am thrown off is %{REQUEST_URI} is used 3 times.


Any help will be greatly appreciated

Thanks

User avatar
Posts: 1264
Joined: 07 Mar 2012, 10:16

Re: back references

11 Apr 2013, 00:28

Hello,

The values that are marked %N are the values of anything inside brackets (...) in RewriteCond. If brackets are inside RewriteRule than it's $N.

Meaning that in this particular case first set of brackets is in "RewriteCond %{HTTP:Host} (.*)", which captures everything in this variable. So %2 would be the value of %{REQUEST_URI} and %3 - %{QUERY_STRING} (.*). More about request processing can be found in these articles:
- http://www.helicontech.com/articles/exploding-myths-about-mod_rewrite-part-i/
- http://www.helicontech.com/articles/exploding-myths-about-mod_rewrite-part-2/

The reason you have three RewriteCond %{REQUEST_URI} in your set is not really relevant. Some people could have combined them in one. First two simply check if requested URL is not looking for favicon or /SomeHeader/ folder.

Regards
Andrew

User avatar
Posts: 3
Joined: 10 Apr 2013, 12:11

Re: back references

11 Apr 2013, 11:25

appreciate it Andrew now it makes sense

just in case someone else revisits this post later. based on your description I think the first set of brackets(Parentheses) is actually in
RewriteCond %{HTTP_COOKIE} ^((?!lastview).)*$ [NC] so based on the above example it would be:

RewriteCond %{HTTP_COOKIE} ^((?!lastview).)*$ [NC] ===========> %1
RewriteCond %{HTTP:Host} (.*) ===========> %2
RewriteCond %{REQUEST_URI} (.*) ===========> %3
RewriteCond %{QUERY_STRING} (.*) ===========> %4

Thanks again for your help :)

Return to ISAPI_Rewrite 3.0

Who is online

Users browsing this forum: No registered users and 9 guests