2

我有我的网站,因为localhost/tutorials/rewrite/news.php此页面上的标题来自我数据库中的新闻文章。每个标题都是阅读文章的链接。链接是

'<a href="read-news.php?url=' . $url . '">' . $title1. '</a>'

read-news.php 页面获取 url 并在 sql 语句中使用它从数据库中获取文章

$url = $_GET['url'];
$sql = "SELECT * FROM news WHERE url = '$url'";

我在 news.php 页面上的链接和 read-news.php 上的 url 看起来像这样

localhost/tutorials/rewrite/read-news.php?url=the-news-today

我怎样才能让它看起来像

localhost/tutorials/rewrite/read-news/the-news-today.php

我使用了以下 htaccess 代码,通过查看我认为应该足以修复它的其他示例

RewriteRule ^read-news/(.*).php /read-news.php?url=$1 [PT]

请提供任何帮助

4

2 回答 2

0

像 Wordpress 那样做。将所有内容重写为 index.php 并从脚本中解析 url。

于 2012-03-21T17:00:44.140 回答
0

编辑:您的 RewriteRule 也需要一些工作。试试这个:

RewriteRule ^read-news/(.*)\.php$ /read-news.php?url=$1 [PT]

(您需要使用“$”来表示重写结束并在“php”之前使用反斜杠转义 .)

此外,您应该链接到您希望它们显示的页面:

href="localhost/tutorials/rewrite/read-news/the-news-today.php"

.htaccess 不会神奇地为您更改网址。

此外,如果您希望能够访问您的查询字符串变量,我会将 QSA 添加到您的标志中:[PT,QSA]

这样你仍然可以访问你的 url 变量$_GET['url']

于 2012-03-23T14:09:26.690 回答