I'm not 100% sure on what you're asking, but, it seems like you want to take that tgpgallery1 and track it through to resulting pages?
modifying your href= would probably take care of that:
Code:
<a href="page2.php?<?php echo $QUERY_STRING;?>">
I don't know if you could do it with auto_append_file, but, you might be able to use auto_prepend_file with an obstart, capture the displayed page, then rewrite the urls on the fly. I wouldn't want to do that on a high traffic site.
It would be trivial to do substitutions with mod_perl or mod_python. A filter mod would allow you to process and pass the query string automatically. Might require some fancy coding to correctly detect href's without parsing the page tree, or, if you coded your links with some simple identifier, i.e. every link was .php?"> you could search for that string occurrence and replace the .php?"> with .php?QUERY_STRING">