This question has already been answered:

Create, I can not figure out how to write a redirect for non-static links, that is, I will take links from the database and redirect them. I have localhost/index.php?id=KMHUd and I want to hide index.php?id= so I could just write localhost/KMHUd . Tell me what you need to register in htaccess?

The script with this question does not work:

  RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_URI} ^(.*)$ RewriteRule .* index.php?id=%1 [L,QSA] 

How to make a redirect in order to be able to use the $_SERVER on the page I do not know.
File index.php:

  <?php $ID = $_GET['id']; if($ID) { try{ $db = new PDO("mysql:host=127.0.0.1;port=3306;dbname=mydb", 'admin', 'admin'); $db->query("SET CHARACTER SET utf8"); $query = $db->prepare("SELECT * FROM linktable WHERE Substitution=?"); $query->bindValue(1, $ID, PDO::PARAM_STR); $query->execute(); $res = $query->fetch(); if($res != null) { $link = 'Location: ' . $res['Link']; //echo "<p>" . $res['Link']; //echo "<p>" . $res['Substitution']; $db = null; // echo $link; header($link); } else echo "<p>Not Found!</p>"; }catch(PDOException $e) { echo $e->getMessage(); } $db = null; } ?> 

Reported as a duplicate by the participants aleksandr barakin , user207618, user194374, Grundy , sercxjo 30 Aug '16 at 8:13 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

  • this does not solve my problem since the script above does not work from this question - Max_Advanced
  • <pre> <code> RewriteEngine on RewriteCond% {REQUEST_FILENAME}! -f RewriteCond% {REQUEST_FILENAME}! -d RewriteCond% {REQUEST_URI} ^ (. *) $ RewriteRule. * index.php? id =% 1 [L, QSA ] </ pre> </ code> - Max_Advanced
  • And why should it work without the appropriate code in index.php ? You specified php and .htaccess tags in your question, but for some reason I see only the code from .htaccess . - Visman
  • already added the index.php code to the post above. - Max_Advanced
  • After header($link); set the exit(); . - Visman

2 answers 2

Use tags for this, you can also make redirects in the PHP header ... And so through htaccess you can make the transition using a variable obtained from the database .... I don’t think that this is possible at all.

    Not sure what the DB is about. Redirect is the easiest here:

     RewriteEngine on RewriteRule ^\/index.php\/(\d+)$ /index.php?id=$1 [QSA] 
    • No, it does not forward ((( - Max_Advanced
    • tried this: RewriteRule ^\/index.html\/(\d+)$ /index.html?id=$1 [QSA] effect is the same - Max_Advanced
    • And where is this index.php - maybe you need to remove the ^ sign at the beginning? - Aleks G
    • no, nothing has changed ( - Max_Advanced