Forum Moderators: phranque

Message Too Old, No Replies

Trying to dynamically control rewriterule flags

         

kman1

9:07 am on Aug 9, 2008 (gmt 0)

10+ Year Member



Hello. I have numerous domains that I would like to be able to quickly make changes to their destination. I have been able to accomplish this with the use of a shared RewriteMap for all domains. In the .htaccess file for each domain, I have an identical RewriteRule that calls the RewriteMap, and passes it's own SERVER_NAME to the RewriteMap. At the RewriteMap, I have each domain name listed in column one, and each desired destination URL in column two. Thus, when the RewriteRule for that domain passes it's name to the RewriteMap, the map passes back the appropriate destination for that domain. It is working just fine, except...

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!

jdMorgan

3:23 pm on Aug 9, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hmmm... I've never tried this, but how about tagging the mapped URL itself with a proxy-or-redirect disposition indicator? The 'tag' could be part of the URL passed back from RewriteMap to the rule. The rule containing the RewriteMap call would then be changed to use no flag, while subsequent rules would be used to do the actual redirect or proxy through-put and strip the proxy-or-redirect indicator from the URL returned by RewriteMap.

# 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]

Here, I assume that internal rewrites will be mapped to /ir/<local-path>, external redirects mapped to /xr/<http://example.com/resource-path>, and proxy through-puts mapped to /pp/http://<example.com/resource-path>.

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

kman1

5:46 pm on Aug 9, 2008 (gmt 0)

10+ Year Member



Jim, you are one smart cookie. That definitely seems to have the potential to accomplish my mission. I did have the idea of appending something to the URL, but didn't come up with a solution of how to extract and use that info. So I'll be tinkering around with your idea for awhile to make it work for me. Thank you so much!

jdMorgan

7:34 pm on Aug 9, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



To answer another of your original questions, I believe the following references should be valid:

# 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}]

These are examples only, and not intended as working code. They just demonstrate accessing RewriteMaps with RewriteConds and using the RewriteRule "[E=]" flag.

Jim