簡體   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