How to make a redirect from any URL to any one page, while at the same time knowing the address that was originally requested?

let's say they write to url http://mysite.ru/blablabla this redirects to http://mysite.ru/index.php, and we get http://mysite.ru/blablabla there

Refinement. It is necessary to make a similarity to http://goo.gl/, so that a short URL on my domain, such as http://mysite.ru/blablabla, redirects to an external URL that is stored in the database. How to do it better?

    2 answers 2

    We create .htaccess with contents:

    #RewriteEngine On RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*) redirect.php?%{QUERY_STRING} 

    Create a file redirect.php with contents

     $dir_name = '/link'; //корневая дирректория if(!empty($_SERVER['REDIRECT_URL'])) // Проверка на редирект { $redirect = preg_replace("#$dir_name/#", "", $_SERVER['REDIRECT_URL']); //получение параметра //дальше подключаем базу данный и ищем поле в таблице $conn = mysql_connect("localhost","username","password"); mysql_select_db("yourDbName",$conn); $sql = 'вид запроса'; if (!($row = mysql_query($sql))) { print('SQL EROR'); } if($row) //если поле найдено то выполняем редирект на url { $res = mysql_fetch_array($row); if(!empty($res['url'])) { header("Location: {$res['url']}"); //$res['url'] - вывод после выполнения sql вашего поля с ссылками }else{ header("Location: {$dir_name}/index.php"); exit; } }else{ //если поле не найдено то выполняем редирект на главную страницу header("Location: {$dir_name}/index.php"); exit; } }else{ //если редирект не найден header("Location: {$dir_name}/index.php"); exit; } 

    I hope this example will help, threw it by hand, so that errors are possible

      Well, you would have said that you are sending somewhere like external.php; well, in htacces, write down how to go, in a script you associate your blblabla with the address base and header ('location: your external link');