2

我有一个 php 文件,它获取存储在数据库中的文章的 id(来自 GET 变量)并使用它来显示它的全部内容,我的意思是,标题、作者、内容等。
当它发生时,网址显示:

localhost/inicio.php?id=1  

其中 id=1 是数据库中的文章 ID。但我希望它显示:

 localhost/this-is-the-article  

我知道我应该编辑 .htaccess 以这种方式重写 url:

RewriteEngine on  
RewriteRule ([a-zA-Z0-9_-]+) inicio.php?id=$1 

在这一点上,我不明白的是我应该在显示文章的 php 文件顶部做什么,当通过 id$_GET['id']来重写 url 时,请记住,如果 id 不同,重写的 url 会改变也,说,这样

localhost/this-is-another-article
4

2 回答 2

1

创建url字段并直接存储 URL,如

this-is-article
this-is-another-article
.... so on

然后通过url而不是id.

localhost/inicio.php?url=this-is-article
localhost/inicio.php?url=this-is-another-article

然后重写它...

Options +FollowSymLinks
RewriteEngine on
RewriteRule ^(.*)$ /inicio.php?url=$1

现在通过

localhost/this-is-article
localhost/this-is-another-article
于 2013-03-19T12:44:43.783 回答
0

您需要的基本上是将 URL 存储在您的数据库中并基于该 URL 而不是 id 进行返回获取。

这是因为无法在 .htaccess 文件中映射 id 和 url,您将在其中编写重写代码所以您的 .htaccess 将类似于:

RewriteEngine on
RewriteRule ^(.*)$ /inicio.php?article_url=$1

在您的 php 代码中,您只需使用这个新创建的 $_GET["article_url"] 变量从数据库中获取信息。

于 2013-03-19T12:47:17.417 回答