These rewrite rules don’t seem to work. Is it just not possible or am I missing something
The problematic rule is the second one, notice the back-reference %1
RewriteCond %{REQUEST_URI} ^/([a-zA-Z\.]+)/.*$ [NC]
RewriteCond %{HTTP_COOKIE} route_controller_%1=([^;]+) [NC]
RewriteRule ^(.*)$ http://example2.com/%1/_/$1 [L]
This is what I am expecting to happen. Given the URL
http://example.com/abc/home
The first rule should store abc in %1
I want the second rule for a cookie called route_controller_abc and if found
Then the third rule would rewite to
http://example2.com/thevalueofthecookie/_/home
But the seconf rule doesn’t seem to be using the %1 back-reference.
Any Ideas?
The reason for this is that string substitution only occurs in the first parameter on a cond and not on its rexexp. You have to do a hack to simulate parameters in regexps using
\1etc.You can also pick up the directory in the rule regexp, since the execution order is rule regexp, cond1, cond2,… rule substitution. eg.
^(*.?)/.*$forabc/homewill set$1toabcand $0 toabc/home. Hence I would try something like:Note that the controller is now in %2 as %1 is used to bind the directory in the cond regexp.
Hope this helps and answers your direct Q. Hovever, I would be a little twitchy using unvalidated cookies like this for redirection. If I were doing this I would either tighten the validation of the parameter
([^;]+)or move this to a small PHP redirector script and do the validation there 🙂