Home  >  Q&A  >  body text

Rewrite the header to: Change .htaccess router to convert id to header

I am using PHP for my work. My htaccess file is as follows:

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

#Redirecciones
#Redirect 301 / /index.php

# Quickregister Rules
ErrorDocument 404 /error.php

Now, to access the news, the routing should look like this:

http://localhost/news/3

I want to change access to the following method:

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

I tried the following rewrite rules without success:

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 days ago392

reply all(1)I'll reply

  • P粉205475538

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

    You can use the following rules:

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

    This will support the following URIs:

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

    The mode used is:

    • ^:Start
    • (news): Match and group news
    • /:match/
    • (?:.*/)?: Matches any text followed by /. This is an optional match
    • (\d ): Match 1 or more digits in capturing group #2
    • /?$: Matches optional /
    • at the end

    reply
    0
  • Cancelreply