Guys, help me out, I have been fighting for almost a month over the output of the CNC from MySQL using PHP. We have a link like сайт.ru/news.php?id_news=1 ? through Rewrite made a link like сайт.ru/1

What code need to register in index.php to output from the database? Now I have, but it does not work:

 <?php $result = $_SERVER['REQUEST_URI']; if (preg_match("/([^a-zA-Z0-9\.\/\-\_\#])/", $result)) { header("ХТТП/1.0 404 Not Found"); echo "Недопустимые символы в URL"; exit; } /* отбрасываем из ЧПУ всё лишнее, оставляя только имя виртуального html-файла. В случае с yourdomain.com/name-page.html это будет name-page функция preg_split формирует массив, разбивая переданную строку по заданной маске. */ $array_url = preg_split("/(\/|\..*$)/", $result, 1, PREG_SPLIT_NO_EMPTY); if (!$array_url) { $id_news = 10; } else { $sef_value = $array_url[0]; /* Далее идёт запрос в БД о наличие в столбце SEF строки $sef_value при положительном ответе получаем из БД соответствующий $sef_value $ID_page, если такой строки не найдено — выводим страницу ошибки 404. */ } /* Теперь обычная обработка, как если бы $ID_page был получен методом GET */ require_once("config.php"); $query = "SELECT * FROM news WHERE chpu = '" . $sef_value . "' LIMIT 1"; $result = mysql_query($query); if ($row = mysql_fetch_array($result)) { $id_news = $row['id_news']; } else { header("HTTP/1.0 404 Not Found"); echo "Страница не существует"; exit; } ?> 

ps chpu - column in the database where the cnc is stored. I need to take the url of the сайт.ru/s-chpu-luchhe/ type page from MySQL, and mod_rewrite is just to cut a persimmon ( news.php?id_news= ) from the existing сайт.ru/news.php?id_news=1 .

  • @nero, To format a code, select it with the mouse and click on the button 101010 of the editor. - Nicolas Chabanovsky
  • one
    But is not it easier to google on the topic [.htaccess beautiful url] [1]? =) [1]: google.com/… - Alexander Molofeev 4:14 pm


1 answer 1

A more practical approach to using Rewrite is:

Narimer for apache:

 <IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ index.php [QSA,L] </IfModule> 

Thus, all requests will be redirected to index.php, and in it we will be clearing $ _SERVER ['REQUEST_URI'] using PHP tools. But it is better to use the ready framework;)

  • I mean it, but when I redirect to the index.php, the code that I have presented above refuses to work - nero