首頁  >  問答  >  主體

將標題重寫為:更改.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 天前391

全部回覆(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
  • 取消回覆