These appendages you ask about is the actual so called query part of an URI:
<scheme>://<authority><path>?<query>
foo://example.com:8042/over/there?name=ferret#nose
\_/ \______________/\_________/ \_________/ \__/
| | | | |
scheme authority path query fragment
Taken from: 3. Syntax Components (RFC 3986) https://www.rfc-editor.org/rfc/rfc3986#page-16
You then need a helper function that is append the (optional) <query> to an existing <scheme>://<authority><path> part. I ignore the <fragment> for this exmaple, as it would needed to be added at the end and I want to leave something as an exercise:
function href_append_query($href)
{
$query = isset($_SERVER['QUERY_STRING'])
? '?' . $_SERVER['QUERY_STRING']
: ''
;
$query = strtr(
$query, [
'"' => '"',
"'" => ''',
'&' => '&'
]
);
return $href . $query;
}
And it's usage:
<a href="<?=href_append_query('http://step2.com/')?>Some link</a>
This little function will ensure that the exisitng QUERY_STRING which can be obtained via $_SERVERDocs is encoded for HTML output.
$_SERVER['QUERY_STRING']