Score:0

nginx url rewrite regex with special characters

in flag

How to write a regex that matches a path node with reserved characters like '+','-' ? for example:

https://e.example.com/foo+/bar/file/test.txt need to be re-written as https://e.example.com/bar/file/test.txt

I tried rewrite ^/foo+(/.*)$ break; but it couldn't match the string.

Any suggestions?

Score:0
us flag

In nginx rewrite directive uses the normalized URI for matching. Normalized URI does not include query arguments or indexed search arguments.

You might be able to use a map here:

map $request_uri $filefromarg {
    ^[^+]*\+(.*)$ $1;
    default $request_uri;
}

And in the server block you would have:

try_files $filefromarg $uri =404;

The map takes $request_uri, which contains the full path to resource and query arguments. The first line regular expression captures everything after + sign and sets it to $filefromarg variable.

Then, in the try_files section that is used as the path to check for resource to serve.

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.