Rewrite the header to: Change .htaccess router to convert id to header
P粉464082061
P粉464082061 2024-01-16 19:15:07
0
1
386

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粉464082061
P粉464082061

reply all(1)
P粉205475538

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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!