简体   繁体   English

正则表达式匹配域/ u和域/ u /用户名,但不匹配域/用户

[英]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 域/ u =域/user.html
  • domain/u/usermame = domain/user.html?u=username 域/ u / usermame =域/user.html?u=用户名
  • domain/u/ = domain/user.html 域/ u / =域/user.html

The following should not be matched: 以下内容匹配:

  • domain/user 域/用户
  • domain/u(any other character(s) here eg universe | uvwxyz) 域/ u(此处的任何其他字符,例如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 . 我正在尝试在.htaccess中使用mod_rewrite将domain/user.html?u=xxx转换为domain/u/xxx

How about this one: 这个怎么样:

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

The user name is captured by the named group username 用户名是由名为组捕获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 您可能已经注意到,第一行来自Cyber​​Dude的答案,我添加了第二行以防止example.com/u和example.com/u/仅仅进行404'ing

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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