首页 > 解决方案 > 如何通过 htaccess 将 id 重写为名称

问题描述

我正在尝试通过 htaccess 重写标题 ID,但它不起作用。我在 htaccess 中使用了几个不同的代码,但它不起作用。当我按名称访问 url 时,它没有显示任何帖子,但使用 ID 时效果很好。

我在 htaccess 中使用此代码:

RewriteRule ^([0-9a-z]+)/?$ news-details.php?nid=$1 [NC,L,QSA]

帖子/类别的 ID:

127.0.0.1/new/news-details.php?nid=89
127.0.0.1/new/category.php?catid=9

标签: .htaccessurl-rewriting

解决方案


Yes it works through the id "/new/89" you mentioned but not with url "/new/89/name-news"

然后将规则(正则表达式)更改为允许/<id>/<slug>(没有尾部斜杠),例如:

RewriteRule ^(\d+/[\w-]+)$ news-details.php?nid=$1 [NC,L,QSA]

<id>/<slug>nid然后作为URL 参数的值过去。


推荐阅读