PHP and htaccess weird issue
<p>I'm working on a project, and I want my link to be like /cars/add (Routing) for example,
and I wrote a function that got the link and processed it to be as above and wrote it to htaccess. So far everything goes well, but the weird thing is the htaccess is not working unless I add a space or new line.</p>

<p>Here is my test:</p>

<pre><code>function getLink() {
  $link =  $_SERVER['REQUEST_URI'];
  $base = dirname($link);

  $flink = str_replace($base,'',$link);
  $flink = str_replace('/','',$flink);
  $tor = $flink;
  $flink = str_replace('.php?action=','/',$flink);

  if (strpos($link,'.php')) {

    whtaccess($tor,$flink);
  }
}

function whtaccess($link,$rlink) {
  $filename = ".htaccess";
  $f = fopen($filename,'a+');
  $string = sprintf("\rRewriteRule %s %s",$rlink,$link);

  $htacess = file_get_contents($filename);
  if (strpos($htacess,$string)==true) {
    fclose($f);
    header("Location:".$rlink);
  } else {
    fwrite($f,$string);
    fclose($f);

    header("Location:".$rlink);
  }
}
</code></pre>

<p>Do you know what can help me?</p>