You cannot use a server variable in a regular-expressions pattern.
In fact, there is no universal way to compare two variables in mod_rewrite.
The best solution is to pick one or the other variable, test it, and then if appropriate, rewrite the request to a script that can compare the two variables and take the desired action.
If that is not desirable, then you can try using "atomic back-references" to 'compare' the two variables based on commutativity. That is, there is no direct 'compare' function, but if var1+var2 == var1+var1, then var1== var2. Atomic back-references are only available in later version of POSIX regex, and in PCRE. So be aware that code that depends on them will fail if ported to an older (Apache 1.3.x) server.
Example:
# If request has not already been rewritten
RewriteCond $1 !^index\.php$
# and if the cookie is not blank and is not equal to the query string
RewriteCond %{HTTP_COOKIE}>%{QUERY_STRING} !^([^>]+)>\$1$
# then rewrite the request to index.php
RewriteRule ^(.*)$ index.php [QSA,L]
This example will not rewrite if the cookie is blank, so be sure that that is what you want.
Further, this example will rewrite requests for *all* types of resources if the cookie is not blank and the cookie and query string do not match exactly. So, your script will have to handle all requests, including those for images, CSS files, JavaScript files, robots.txt, sitemap.xml, etc. Again, be sure that that is what you intended.
Otherwise, you will need to modify this example code and/or add additional RewriteConds to do what you want...
Again, this example code will only work on servers that support atomic back-references, and some do not.
Jim