mod_rewrite 的正则表达式仅采用之前的内容 --

发布于 2024-11-01 17:27:55 字数 332 浏览 1 评论 0 原文

嗨,我有一个网址,读起来就像

/users/nick--last-first-middle-bla-bla-bla.html

我需要的是使正则表达式接受尼克,即使它像这样

ni-ck--last-first-middle-bla-bla-bla.html
  1. bla-bla可能是任意数量的变量,我只需要获取“nick”部分,无论它后面是什么,因为它总是分开的经过 ” - ”。
  2. 昵称最多 50 个字符

有人可以建议使用正则表达式进行 mod 重写吗,谢谢。

Hi i have url that reads like

/users/nick--last-first-middle-bla-bla-bla.html

what i need is to make regex that takes nick even if its like this

ni-ck--last-first-middle-bla-bla-bla.html
  1. bla-bla might be any number of variables i just need to get "nick" part regardless what follows it because it will be always separated by "--".
  2. nick only up to 50 characters

Can someone suggest working regex for mod-rewrite, thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

城歌 2024-11-08 17:27:55

选项 1:

如果 URL 始终具有昵称、最后一个、第一个和中间,后跟可选变量,并且昵称必须为 1 到 50 个字符,则此测试规则将起作用。

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteRule ^users/((?:(?!--)[^/]){1,50})--([^-/]+)-([^-/]+)-([^-/]+)([^/]*)$ index.php?nick=$1&last=$2&first=$3&middle=$4&vars=$5 [L]
 </IfModule>

给定:/users/ni-ck--last-first-middle-bla-bla-bla.html 此规则分离并提供以下 $_GET 数组变量:

Array
(
    [nick] => ni-ck
    [last] => last
    [first] => first
    [middle] => middle
    [vars] => -bla-bla-bla.html
)

选项 2:

如果 URL 始终具有 1 到 50 个字符的昵称,后跟 "--" 并且后面的所有内容都是可选的,则此测试规则将起作用。 (这个没有分离出名称部分):

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteRule ^users/((?:(?!--)[^/]){1,50})--([^/]*)$ index.php?nick=$1&vars=$2 [L]
</IfModule>

给定: /users/ni-ck--last-first-middle-bla-bla-bla.html 此规则分离并提供以下内容$_GET 数组变量:

Array
(
    [nick] => ni-ck
    [vars] => last-first-middle-bla-bla-bla.html
)

Option 1:

If the URL will always have a nick, last, first and middle, followed by optional vars and the nick must be from 1 to 50 chars, then this tested rule will work.

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteRule ^users/((?:(?!--)[^/]){1,50})--([^-/]+)-([^-/]+)-([^-/]+)([^/]*)$ index.php?nick=$1&last=$2&first=$3&middle=$4&vars=$5 [L]
 </IfModule>

Given: /users/ni-ck--last-first-middle-bla-bla-bla.html this rule separates out and provides the following $_GET array variables:

Array
(
    [nick] => ni-ck
    [last] => last
    [first] => first
    [middle] => middle
    [vars] => -bla-bla-bla.html
)

Option 2:

If the URL will always have a nick from 1 to 50 chars, followed by "--"and everything following that is optional, then this tested rule will work. (This one does not separate out the name parts):

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteRule ^users/((?:(?!--)[^/]){1,50})--([^/]*)$ index.php?nick=$1&vars=$2 [L]
</IfModule>

Given: /users/ni-ck--last-first-middle-bla-bla-bla.html this rule separates out and provides the following $_GET array variables:

Array
(
    [nick] => ni-ck
    [vars] => last-first-middle-bla-bla-bla.html
)
夜唯美灬不弃 2024-11-08 17:27:55
/users/((?:-?[0-9a-zA-Z]){,50})

应该用 1 美元捕获你的昵称。 (不记得非捕获括号在 mod-Rewrite 中是否有效,否则只是丢失 ?:

/users/((?:-?[0-9a-zA-Z]){,50})

Should capture your nick in $1. (Don't remember if the non capturing parenthesis work in mod-Rewrite, or else just loose the ?: )

早茶月光 2024-11-08 17:27:55

^/users/(.{0,50}).*(?=--)(?

^/users/:匹配起始字符串
(.{0,50}):将任意字符(最多 50 个)匹配到该组中
.*:匹配第50个之后的任意字符
但仅匹配到--(不包括在内):
(?=--):正向先行,-- 紧随此位置之后
(?

请注意,-- 是必需的,否则模式将不匹配。

^/users/(.{0,50}).*(?=--)(?<!--.*) matches the "nick" part up to 50 characters or up to -- (whichever is shortest) in a group, using quantifiers and lookarounds for the constraints.

^/users/: match the start string
(.{0,50}): match any character, up to 50, into this group
.*: match any character after the 50th
but only match until -- (not inclusive):
(?=--): positive lookahead, -- exists immediately after this location
(?<!--.*): negative lookbehind, -- does not exist anywhere before this location

Note that the -- is required or the pattern will not match.

心凉 2024-11-08 17:27:55

^/users/(?(.{0,50}?--)(.*?)|(.{50}).+)-- 不适用于环视,而是使用惰性量词和条件(我不知道 mod_rewrite 是否支持)将短昵称放入组 1 或将截止昵称放入组 2,因此您可以连接这些组以获得请求的昵称。

^/users/:匹配起始字符串
(?:条件表达式的开始
(.{0,50}?--):[if] 检查这里是否有短昵称(第一个 -- 之前不超过 50 个字符) )
(.*?):[then] 将任何字符匹配到该组中(直到第一个 --,见下文,由于延迟匹配)
|:条件表达式中的分隔符
(.{50}).+:[else] 仅匹配该组中的前 50 个字符
):条件表达式结束
--:最终匹配


实际上并不清楚如何处理太长的缺口。如果长缺口可以被忽略而不是被切断,那么也许以下就足够了?

^/users/(.{0,50}?)-- 仅匹配第一组中的短昵称(惰性量词)...

^/users/(?(.{0,50}?--)(.*?)|(.{50}).+)-- does not work with lookarounds but instead uses lazy quantifiers and a conditional (I do not know if either is supported by mod_rewrite) which puts either the short nick in group 1 or the cut off nick in group 2, so you can just concatenate those groups to get the requested nick.

^/users/: match the start string
(?: start of the conditional expression
(.{0,50}?--): [if] check if we have a short nick here (no more than 50 characters before first --)
(.*?): [then] match any character into this group (until first --, see below, due to lazy matching)
|: delimiter in the conditional expression
(.{50}).+: [else] only match first 50 characters into this group
): end of the conditional expression
--: final match


Actually it is not really clear what to do with nicks that are too long. If long nicks can just be ignored instead of cut off, then maybe the following suffices?

^/users/(.{0,50}?)-- only matches short nicks in the first group (lazy quantifier)…

相权↑美人 2024-11-08 17:27:55

尝试在 .htaccess 文件中遵循以下规则:

RewriteEngine on
Options +FollowSymlinks -MultiViews

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^users/(.{1,50}?)-- /foo.php?name=$1 [L]

测试

如果我有这样的 foo.php:

<?php var_dump($_SERVER["QUERY_STRING"]); ?>
  1. For the URL of /users/nicked-ic-ic-ack--last-first-middle-bla-bla -bla.html 它打印:

    string(21) "name=nicked-ic-ic-ack"

  2. 对于 /users/ni-ck--last-first-middle- 的 URL bla-bla-bla.html 它打印:

    string(10) "name=ni-ck"

  3. 对于 /users/nick--last-first-middle-bla-bla-bla.html 的 URL,它打印:

    string(9) "name=nick"

  4. 对于 URL /users/nick--last--first (多个 --)它打印:

    string(9) "name=nick"

  5. 对于 /users/nicked-ic-ic-nicked-nicked-nicked-nicked-nicked-nicked-nicked- 的 URL ack--last-first-middle-bla-bla-bla.html抛出404,因为名称长度现在>; 50

    404 - 未找到

Try following rule in your .htaccess file:

RewriteEngine on
Options +FollowSymlinks -MultiViews

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^users/(.{1,50}?)-- /foo.php?name=$1 [L]

TESTING

If I have foo.php like this:

<?php var_dump($_SERVER["QUERY_STRING"]); ?>
  1. For the URL of /users/nicked-ic-ic-ack--last-first-middle-bla-bla-bla.html it prints:

    string(21) "name=nicked-ic-ic-ack"

  2. For the URL of /users/ni-ck--last-first-middle-bla-bla-bla.html it prints:

    string(10) "name=ni-ck"

  3. For the URL of /users/nick--last-first-middle-bla-bla-bla.html it prints:

    string(9) "name=nick"

  4. For the URL /users/nick--last--first (multiple --) it prints:

    string(9) "name=nick"

  5. For the URL of /users/nicked-ic-ic-nicked-nicked-nicked-nicked-nicked-nicked-nicked-ack--last-first-middle-bla-bla-bla.html it throws 404 since length of name is now > 50

    404 - Not found

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文