首页  >  问答  >  正文

将标题重写为:更改.htaccess路由器以将id转换为标题

我正在使用PHP进行工作。 我的htaccess文件如下:

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

#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= [NC,L,QSA]
RewriteRule ^news/([a-zA-Z]+)?$ index.php?url=news&name= [L]
RewriteRule ^news/(.*)$ index.php?url=news&name= [L]

P粉464082061P粉464082061251 天前393

全部回复(1)我来回复

  • P粉205475538

    P粉2054755382024-01-17 18:15:01

    您可以使用以下规则:

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

    这将支持以下URI:

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

    使用的模式是:

    • ^:开始
    • (news):匹配并分组news
    • /:匹配/
    • (?:.*/)?:匹配任何文本后面跟着/。这是可选匹配
    • (\d+):在捕获组#2中匹配1个或多个数字
    • /?$:在结尾处匹配可选的/

    回复
    0
  • 取消回复