如何使用htaccess重定向页面而不发送id

时间:2013-04-14 20:51:56

标签: php apache .htaccess url-rewriting

我遇到了.htacces网址重写的问题。

我有一个名为dir的文件夹,其中有三个文件


enter image description here


.htaccess为空,i.php也为空 index.php我有

 $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>';

        }

当我将鼠标悬停在echo输出时,会显示

这样的链接

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

所以,当我点击它时,它会带我到我想要的地方,但我需要摆脱id,在这种情况下是101我希望链接只有localhost/dir/i/today-in-news时我点击它。我尽我所能,但没有任何结果就失败了。

1 个答案:

答案 0 :(得分:1)

此代码会将该标题传递给i.php,然后您可以从数据库或其他任何位置将其拉出。看看here了解一些.htaccess信息。

<强>的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'];