简体   繁体   中英

Regex to match domain/u & domain/u/username but not domain/user

The title says it all, but I will explain a bit more:

The following should be matched:

  • domain/u = domain/user.html
  • domain/u/usermame = domain/user.html?u=username
  • domain/u/ = domain/user.html

The following should not be matched:

  • domain/user
  • domain/u(any other character(s) here eg universe | uvwxyz)

I have got close, but I cannot work out how to only match if it is followed by nothing or a slash, And if it is followed by a slash catch whats after the slash.

I'm trying to use mod_rewrite in a .htaccess to turn domain/user.html?u=xxx into domain/u/xxx .

How about this one:

^domain\/u($|\/$|\/(?<username>.+)$)

The user name is captured by the named group username

I found the solution, It is as follows:

RewriteRule ^http://www.example.com\/u(\/(?<username>.+)$) user.html?u=username [L]
RewriteRule ^(u/$|u$) user.html [NC,L]

As you may have noticed, the first line is from CyberDude's answer, I added the second line to prevent example.com/u & example.com/u/ from simply 404'ing

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM