.htaccess - 如何通过 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
解决方案
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 参数的值过去。
推荐阅读
- r - 获取连接中匹配的行数和总行数
- azure - 如何使用我的 DNS 服务器获取 Azure 容器实例?
- powerapps - 如何使用 url 参数进行调试
- angular - fa-icon 在有效的结束标签上抛出错误
- typescript - Override tsconfig.json in a VS2019 solution/MSBuild?
- tensorflow - 如何在 TensorFlow 2.x 中保存 seq2seq 模型?
- javascript - 根据使用 Google Sheets Apps 脚本在未知行数内过滤关键字移动特定行
- python - 如何绘制点以显示点网格?
- amazon-web-services - CloudFormation:Fn::GetAtt:不适用于 AWS::RDS::DBInstance 上的 Endpoint.Address
- r - 用两种不同的颜色填充 stat_ecdf