URL rewrite with PHP & MySQL and htaccess


URL rewrite with PHP & MySQL and htaccess

我的网站是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]

如有任何帮助,请

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

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

(您需要使用"$"来表示重写结束,并用反斜杠转义.before"php")

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

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

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

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

这样,您仍然可以使用$_GET['url']访问您的url变量。

按照Wordpress的方式操作。将所有内容重写到index.php中,并从脚本中解析url。