Forum Moderators: phranque
My problem is that I can't control the FLAGS at the end of the Rewrite Rule. In some cases, where I am passing back a destination URL that points to one of my other domains, I want to use the [P] flag so that the address bar displays the original URL. In other cases, such as when forwarding to Sedo, I want it to act as a standard redirect and display the new address.
In keeping with my idea of being able to make destination changes "on the fly", I am trying to find a way to not only pass a URL back to the RewriteRule, but also be able to dynamically control what [FLAGS] (if any) that the RewriteRule should use with that particular URL.
The only possible solution I can come up with so far is to create multiple RewriteRules that are identical, except that each would use a different flag. Then, I would have to somehow have a RewriteCond for each rule that checks to see if that rule should be processed. Going on that theory, that leads me to the question of whether or not a RewriteCond can open a database or text file, pass the SERVER_NAME to it, and check to see if I have set the condition to be met. In other words, I could have a secondary file which lists each domain, and which flag the RewriteRule should use, but to be checked by the RewriteCond.
I wish I could explain this better, as I'm sure several heads are spinning right now. I am a novice and I hesitate to ask for help because of my inability to describe this correctly, but now I am all out of ideas. If anyone has any idea of what I'm trying to say or accomplish, and knows of any tricks or anything, I would GREATLY appreciate any advice!
Thanks!
# Look up mapped URL with disposition tag prefix
RewriteRule ^(/some-URL)$ ${my_map:$1}
# Internal rewrite
RewriteRule ^/ir/(.+)$ /$1 [L]
# External redirect
RewriteRule ^/xr/(.+)$ $1 [R=301,L]
# Proxy through-put
RewriteRule ^/pp/(.+)$ $1 [P]
My 'disposition tags' are utterly arbitrary -- pick a tagging method that will result in unambiguous results, no 'collisions' with your current URL-path-naming scheme, and lowest likelihood of being error-prone in your development/maintenance environment.
Beware of a known mod_rewrite bug: If it appears that the above approach results in "mysterious multiple copies" of part of the URL-path-prefix appearing in the rewritten URL, then you've got the bug. A work-around is to avoid actually changing the URL-path until the final RewriteRule. This can be done by putting all 'intermediate' substitutions into server variables, testing them with RewriteConds, manipulating them using the [E=var:val] flag, and not letting RewriteRule modify the originally-requested URL-path until the very last step. This is a rather indirect and 'clunky' work-around, and doubles the number of code lines. But it does work.
Jim
# Get mapped value to %1 variable using RewriteCond
RewriteCond ${my_map:$1} ^(.+)$
#
# Get mapped value to environment variable
RewriteRule (.+) - [E=mapped-value:${my_map:$1}]
Jim