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
.