We do something similar although we don't rewrite the URL, what we do is look up the incoming URL then redirect to a pool but leave the URL alone, our code is:
when HTTP_REQUEST {
Get the requested host header
set req [HTTP::host]
get the actual pool based on the header
set redir [class lookup [HTTP::host] datagroup_name]
check if we have already LB them but looking for our cookie
log the redirect for troubleshooting (enable/disable as needed)
log local0. "Redirecting request for [HTTP::host] to $redir"
direct them to the pool as normal
pool $redir
eval [LB::select]
persist cookie
}