title: Redirection HTTP/1.1 301 Moved Permanently
body: <p>I have the following files. The objective of this is to redirect to the correct news. For example:
  localhost/tostadotv/esto-es-una-noticia-28.html</p>
  
  <p>If I intentionally modify the url, for example:
  localhost/tostadotv/esto-es-una-noticia-modificada-incorrecta-28.html</p>
  
  <p>I should redirect myself to the correct news:
  localhost/tostadotv/esto-es-una-noticia-28.html</p>
  
  <p>However, it redirects me to this:
  <a href="http://localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/esto-es-una-noticia-28.html" rel="nofollow noreferrer">http://localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/esto-es-una-noticia-28.html</a></p>
  
  <p>Where this error? Could you please help me thanks. Excuse my english I'm from Argentina I do not speak English</p>
  
  <p>.htaccess</p>
  
  <pre><code>RewriteEngine On
  RewriteRule ^.*-([0-9]+)\.html$ noticia.php?id_not=$1 [L]
  </code></pre>
  
  <p>noticia.php</p>
  
  <pre><code>&lt;?php require_once("lib/connection.php"); ?&gt;
  &lt;?php require_once("lib/functions.php"); ?&gt;
  &lt;?php
  fix_category_product_url();
  ?&gt;
  </code></pre>
  
  <p>functions.php</p>
  
  <pre><code>function fix_category_product_url() {      
      $proper_url = get_proper_category_product_url(1);
  
      if ( SITE_DOMAIN.$_SERVER['REQUEST_URI'] != $proper_url) { 
          header('HTTP/1.1 301 Moved Permanently'); 
          header('Location: '.$proper_url);
          exit();         
      }
  }
  function get_proper_category_product_url($id) {   
      $product_id = $_GET['id_not'];
  
      $query = sprintf('SELECT titulo FROM noticias WHERE id_not = "%d"', mysqli_real_escape_string($GLOBALS['DB'], $product_id));
      $restit = mysqli_query($GLOBALS['DB'], $query);
      $noticia = mysqli_fetch_array($restit);
  
      $proper_url = make_category_product_url($noticia['titulo'], $product_id, $id);
  
      return $proper_url;
  }
  define('SITE_DOMAIN', 'localhost');
  function _prepare_url_text($string) {    
      $NOT_acceptable_characters_regex = '#[^-a-zA-Z0-9_ ]#';
      $string = iconv('UTF-8','ASCII//TRANSLIT',$string);
      $string = preg_replace($NOT_acceptable_characters_regex, '', $string);
  
      $string = trim($string); 
  
      $string = preg_replace('#[-_ ]+#', '-', $string); 
  
      return $string;
  }
  
  function make_category_product_url($product_name, $product_id, $ido) { 
      $clean_product_name = _prepare_url_text($product_name);
  
      if ($ido == 0)
          $url = strtolower($clean_product_name).'-'.$product_id.'.html'; 
      else 
          $url = SITE_DOMAIN.'/tostadotv/'.strtolower($clean_product_name).'-'.$product_id.'.html'; 
  
      return $url;
  }
  </code></pre>