Rewrite regex with percent signs

Francis Daly francis at daoine.org
Sun May 22 13:42:17 UTC 2016


On Sun, May 22, 2016 at 07:16:35AM -0400, redrobes wrote:

Hi there,

> For example, we have a url of the following:
> /members/redrobes-albums-2d%20vs%203d%20?-picture12345-mt-pub01.jpg

The %20 pieces in there are url-encoded spaces. In a "location" or a
"rewrite", you would have to match a single space character each.

However, there is also a ? in the url; that marks the start of the query
string. A "location" or "rewrite" in nginx will *not* consider that part
of the url.

> it needs to go to
> 
> /attachment.php?attachmentid=12345

It is not immediately clear to me which parts of the original url are
important in deciding whether the request should be redirected or not.

> we have:
> 
> location /members/ {
> rewrite ^/members/.+-albums-.+-picture(\d+)-.*
> /attachment.php?attachmentid=$1? redirect;
> }

That suggests that just those three words matter. You might be able
to put something together involving "$args" matching "-picture(\d+)-"
if the request matches "^/members/.*-albums-", perhaps?

Alternatively, perhaps the thing that created the url in the first place,
incorrectly did not url-encode the ? to %3F.

> and this particular one is not working. It works with many others where the
> original url did not have the %20's in them. So there is something about
> those %20's that are causing these to fail.

I suspect that it is the ? rather than the %20, from the one example
you have given.

> I can write a perl script and run that url through its regex and it does
> change them.
> 
> So what does the nginx regex do different from perl regex with regard to %
> signs.

With regard to % signs, nginx regex uses the %-unencoded version. With
regard to ?, some nginx parts do not consider anything after the ? when
matching.

Good luck with it,

	f
-- 
Francis Daly        francis at daoine.org



More information about the nginx mailing list