Apache URL 重写无法正常工作
我正在尝试使用 apache-rewrite 规则来转换以下 URL:
I am trying to use apache-rewrite rule to convert the below URL:
http://localhost/foo/bar/news.php?id=24
变成这样的格式:
http://localhost/foo/bar/news/foo-bar
数字 24
是 MySQL 表中随机行的 id
,其中还包含 title
和 content代码> 字段.
The number 24
is an id
of a random row from a MySQL table, which also contains title
and content
fields.
MariaDB [blog]> select * from articles;
+----+---------+----------+
| id | title | content |
+----+---------+----------+
| 1 | foo-bar | bla bla |
+----+---------+----------+
1 row in set (0.00 sec)
我的 .htaccess
中有以下规则:
I have the following rule inside my .htaccess
:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
^news/([A_Za_z0_9_]+)$ DIRECTORY/AID/news.php?id=$1 [QSA,L]
我也有一个 php 代码可以生成这样的链接:
I also have a php code that generates a link like this:
$link = "<a href='news.php?id={$row['id']}'></a>";
echo $link;
但是,我无法获得将路径更改为所需最终结果的重写规则.
However, I can't get the rewrite rule to change the path as the desired end result.
推荐答案
首先,您需要更改 html 中的 href,以提供新的 url 格式
First of all, you would need to change the href in the html, to give the new url format
function news_preview() {
$query = "SELECT * FROM news ORDER BY id DESC LIMIT 5 ";
$result = mysql_query($query) or die(mysql_error());
while($row=mysql_fetch_array($result))
{
echo "<a href="/news/$row[id]"> ". substr($row['title'], 0,26)."...</a><br/>".; }
}
将生成像 http://localhost/news/24
请注意,我从 url 中删除了 /DIRECTORY/AID
,因为 htaccess 建议您希望它是 url,而不是您在文本中陈述的内容.
Note that I removed the /DIRECTORY/AID
from the url, as the htaccess suggest you want that to be url, as opposed to what you stated in the text.
但是现在可以访问 http://localhost/news/this_is_article_title
类型的 url.因为 this_is_article_title
和 id 24
之间没有相关性,实现这一点的唯一方法是将 id 也添加到 url,或者让 php 查找数据库中具有此标题的新闻文章.
But now the get to the http://localhost/news/this_is_article_title
type of url. Because there is no correlation between this_is_article_title
and the id 24
, the only way to achieve this is by either adding the id to the url too, or to have the php lookup the news-article with this title in the database.
然而,这最后一个解决方案有一些问题,因为你不能只给我们一个 URL 中的标题.你必须转义字符.此外,您还必须为数据库中的标题行添加索引以获得更好的性能.
This last solution however has some problems, as the you can't just us the title in a url. You have to escape characters. Also you'll have to add a index for the title row in the DB for better performance.
所以我将采用第一个解决方案.我们将生成这样的网址
So I'll go with the first solution. We will generate urls like this
http://localhost/news/24/this_is_article_title
首先是php部分
function news_preview() {
$query = "SELECT * FROM news ORDER BY id DESC LIMIT 5 ";
$result = mysql_query($query) or die(mysql_error());
while($row=mysql_fetch_array($result))
{
$url = "/news/$row[id]/".preg_replace('/[^a-zA-Z0-9-_]/', '_', $row['title']);
echo "<a href="$url"> ". substr($row['title'], 0,26)."...</a><br/>".; }
}
接下来是 htaccess 部分.
Next comes the htaccess part.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^news/([0-9]+)/([A-Za-z0-9_-]+)$ DIRECTORY/AID/news.php?id=$1 [QSA,L]
我认为应该可以.
相关文章