user1236473
user1236473

Reputation:

How to redirect page with htacess without sending id

I have a problem with .htacces url-rewritting.

I have a folder called dir and in it I have three files


enter image description here


.htaccess is empty, i.php is empty too but in index.php I have

 $sql = mysql_query("SELECT * FROM clients ORDER By id DESC LIMIT 10"); 
        while($row = mysql_fetch_array($sql)){

        $url = "i/$row[id]/".preg_replace('/[^a-zA-Z0-9-_]/', '-', $row['company']);
        echo "<a href='".$url ."'>".$row['company'].'<br/ ></a>';

        }

When I hover over echo outputs a link like

localhost/dir/i/101/today-in-news

localhost // is localhost obviously
dir // is a folder where I keep index.php and i.php files
i // is i.php
101 // is the id of the news
today-in-news // is the title of the news

So, when I click on it, it takes me where I want, but I need to get rid of the id which in this case is 101 I want the link only to be localhost/dir/i/today-in-news when I click on it. I have tried everything I could, but failed without any result.

Upvotes: 0

Views: 105

Answers (1)

user835223
user835223

Reputation:

This code will pass that title through to i.php where you can then pull it from the database or whatever. Take a look here for some .htaccess information.

index.php:

$sql = mysql_query("SELECT * FROM clients ORDER By id DESC LIMIT 10"); 
while($row = mysql_fetch_array($sql)){
    $url = "i/".preg_replace('/[^a-zA-Z0-9-_]/', '-', $row['company']);
    echo "<a href='".$url ."'>".$row['company'].'<br/ ></a>';
}

.htaccess:

<IfModule mod_rewrite.c> 
    RewriteEngine On
    RewriteRule ^i\/([a-zA-Z0-9_-])+\/?$ i.php?title=$1 [L]
</IfModule> 

i.php

$title = $_GET['title'];

Upvotes: 1

Related Questions