Score:0

Regular expression for url longer than 3 characters Nginx

bm flag

There is a website site.ru/ I need to write a regular expression where pages longer than 3 characters only work after the first slash. If in this case there are other pages after the slash, then 404 was returned. For example

site.ru/demo -> site.ru/demo
site.ru/dem/dem -> site.ru/dem/dem
site.ru/demo/{string of any length} -> 404

I wrote the following regular expression,

map $example {
            default 0;
            "~[^\/]\w.{3,}" 1;
}

but it works on both site.ru/demo and site.ru/demo/demo. Where is my mistake?

Richard Smith avatar
jp flag
`/demo/demo` is probably a bad example, which one causes it to fail? `/demo/dem` or `/dem/demo`?
All In avatar
bm flag
@Richard Smith added update: can be a string of any length
Richard Smith avatar
jp flag
Try: "~^/\w{4,}/"
William Martens avatar
in flag
I don't know but this grabs urls; can be a good starting point I guess; `"(http|https)://[a-zA-Z0-9./?=_%:-]*"` `tested in bash`
us flag
Can you elaborate where the `$example` variable is used? That affects what has to be considered when writing the regex? Also, you can use https://regexr.com to play with the regular expressions.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.