title: How to use $_SERVER['REQUEST_URI'] to write a new URL and not infinitely-loop?
body: <p>I have a php page and want to write a link that is based on the current URL.  Like so:</p>
  
  <pre><code>$alias = substr(strrchr($_SERVER['REQUEST_URI'], "/"), 1);
  
  print '&lt;a href="'.$alias.'&amp;sort=name"&gt;Sort by Name&lt;/a&gt;';
  </code></pre>
  
  <p>On the page <a href="http://domain.com/listings" rel="nofollow noreferrer">http://domain.com/listings</a> , the link is written to <a href="http://domain.com/listings&amp;sort=name" rel="nofollow noreferrer">http://domain.com/listings&amp;sort=name</a> , which works perfectly.</p>
  
  <p>But then once I am on the page <a href="http://domain.com/listings&amp;sort=name" rel="nofollow noreferrer">http://domain.com/listings&amp;sort=name</a>, the link on the page is written to <a href="http://domain.com/listings&amp;sort=name&amp;sort=name" rel="nofollow noreferrer">http://domain.com/listings&amp;sort=name&amp;sort=name</a>.  Additionally, if I click one of the other sorting links I will end up with <a href="http://domain.com/listings&amp;sort=name&amp;sort=comments" rel="nofollow noreferrer">http://domain.com/listings&amp;sort=name&amp;sort=comments</a> .</p>
  
  <p>That is dorky.  Is there a better way to generate my link?  Or should I do something to clean $alias before I reuse it?  Complication:  There are some variables (like <code>&amp;category=34</code> and <code>&amp;title=spa</code>) that I <em>don't</em> want to remove from the URL, so I can't just clean it back to <code>/listings</code>.</p>
  
  <p>(I am using Drupal, if there are secret variables built in there that would be cool too.)</p>