繁体   English   中英

将.htaccess路由器,id改为title

[英]Change the .htaccess router, id to title

我正在使用 PHP。 我的 htaccess 看起来像这样:

RewriteEngine On
RewriteRule ^news/([a-zA-Z0-9_-]+)(|/)$ index.php?url=news&id=$1

#Redirecciones
#Redirect 301 / /index.php

# Quickregister Rules
ErrorDocument 404 /error.php

现在在这种情况下访问新闻的路线是这样的:

http://localhost/news/3

我想改变它来访问一些如下

http://localhost/news/mi-noticia-nueva
http://localhost/news/mi-noticia-nueva/3

我尝试了以下重写规则但没有成功:

RewriteRule ^news/(\d+/[\w-]+)$ index.php?url=news?id=$1 [NC,L,QSA]
RewriteRule ^news/([a-zA-Z]+)?$ index.php?url=news&name=$1 [L]
RewriteRule ^news/(.*)$ index.php?url=news&name=$1 [L]

您可以使用此规则:

RewriteRule ^(news)/(?:.*/)?(\d+)/?$ index.php?url=$1&id=$2 [L,QSA,NC]

这将支持那里的 URI:

/news/mi-noticia-nueva/3
/news/3

使用的模式是:

  • ^ : 开始
  • (news) : 比赛和分组news
  • / :匹配一个/
  • (?:.*/)? : 匹配任何后跟/的测试。 这是可选匹配
  • (\d+) :匹配捕获组 #2 中的 1+ 个数字
  • /?$ :匹配可选的/在结束之前

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM