title: PHP rewrite url but keep anchor tags
body: <p>Is it possible to rewrite a URL that contains an #anchor tag - and append that #anchor tag to the new url?</p>
  
  <p>In my news php script, I check to make sure the request_uri matches the data I have stored the the MySQL database. If not, I send a 301 header and header location to the correct URL.</p>
  
  <pre><code>$uri = $_SERVER['REQUEST_URI'];
  
  // Grab the $sid and $url stored in the MySQL table and set it as $realurl
  $realurl = "/$sid/$url/";
  
  if ($uri != $realurl)
  {
      Header( "HTTP/1.1 301 Moved Permanently" );
      Header( "Location: $realurl" );
      exit;
  }
  </code></pre>
  
  <p>This has been working great for me, but the problem is that I have recently added a comments php script to my news pages. Depending on what is being done, it could append #comments or #comment_form to the url.</p>
  
  <p>Is there any way to modify the above code so if $url and $realurl does not match - header location to the $realurl + keep any #anchor tag that may have been present?</p>
  
  <hr>
  
  <p><strong>Problem Solved</strong> - not sure how or why it works, but all I ended up doing was changing :</p>
  
  <pre><code>if ($uri != $realurl)
  </code></pre>
  
  <p>to</p>
  
  <pre><code>if (stristr($uri, $realurl) === false)
  </code></pre>
  
  <p>If the $uri does not contain my $realurl, the url is rewritten and #anchor tag still remains.</p>