Welcome! Log In Create A New Profile

Advanced

nginx seems to treat %3F as "?"

Posted by ST 
ST
nginx seems to treat %3F as "?"
November 22, 2017 04:40PM
Hello,

I have following redirection rule defined:

location ~ "^/(.*)\.html[^\?]+" {
return 301 /$1.html;
}

so that everything besides "?" after an URL gets truncated:
like
example.com/test.html%D1%80%D0%BE%D1%80%D0%BB -> example.com/test.html

however it doesn't work when "?" is url encoded into %3F. I would like
example.com/test.html%3F to redirect to example.com/test.html

Is it possible somehow?

Thank you!

_______________________________________________
nginx mailing list
nginx@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx
Francis Daly
Re: nginx seems to treat %3F as "?"
November 22, 2017 11:20PM
On Wed, Nov 22, 2017 at 05:34:13PM +0200, ST wrote:

Hi there,

> I have following redirection rule defined:
>
> location ~ "^/(.*)\.html[^\?]+" {

That says: /anything.html followed by one-or-more things that are not ?.

Note that "location" works on the unencoded url version, and does not
include the ? that marks the query string, or anything after it.

> return 301 /$1.html;
> }
>
> so that everything besides "?" after an URL gets truncated:

No.

*everything* after .html gets removed in the 301 response, provided
that there is something immediately after .html that is not a ? (which
would be %3F in the original url, because ? is special).

> like
> example.com/test.html%D1%80%D0%BE%D1%80%D0%BB -> example.com/test.html

The thing immediately after .html is the unencoded version of %D1, which is
not ?, so the location matches and the rewrite happens.

> however it doesn't work when "?" is url encoded into %3F. I would like
> example.com/test.html%3F to redirect to example.com/test.html

Your location block explicitly excludes that case.

Why?

As in: I do not understand what the thing that you are trying to achieve
is. Can you explain it? Perhaps with examples of some things that should
be rewritten and some things that should not be?

At a guess, perhaps

location ~ ^/(.*)\.html. {

is what you want? Starts with /, includes .html, and is followed by anything.

That should match /a.htmlx and /a.html%3Fx, but not /a.html or /a.html?x=y

f
--
Francis Daly francis@daoine.org
_______________________________________________
nginx mailing list
nginx@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx
ST
Re: nginx seems to treat %3F as "?"
November 23, 2017 10:30AM
On Wed, 2017-11-22 at 22:11 +0000, Francis Daly wrote:
> On Wed, Nov 22, 2017 at 05:34:13PM +0200, ST wrote:
>
> Hi there,
>
> > I have following redirection rule defined:
> >
> > location ~ "^/(.*)\.html[^\?]+" {
>
> That says: /anything.html followed by one-or-more things that are not ?.
>
> Note that "location" works on the unencoded url version, and does not
> include the ? that marks the query string, or anything after it.
>
> > return 301 /$1.html;
> > }
> >
> > so that everything besides "?" after an URL gets truncated:
>
> No.
>
> *everything* after .html gets removed in the 301 response, provided
> that there is something immediately after .html that is not a ? (which
> would be %3F in the original url, because ? is special).
>
> > like
> > example.com/test.html%D1%80%D0%BE%D1%80%D0%BB -> example.com/test.html
>
> The thing immediately after .html is the unencoded version of %D1, which is
> not ?, so the location matches and the rewrite happens.
>
> > however it doesn't work when "?" is url encoded into %3F. I would like
> > example.com/test.html%3F to redirect to example.com/test.html
>
> Your location block explicitly excludes that case.
>
> Why?
>
> As in: I do not understand what the thing that you are trying to achieve
> is. Can you explain it? Perhaps with examples of some things that should
> be rewritten and some things that should not be?
>
> At a guess, perhaps
>
> location ~ ^/(.*)\.html. {
>
> is what you want? Starts with /, includes .html, and is followed by anything.
>
> That should match /a.htmlx and /a.html%3Fx, but not /a.html or /a.html?x=y

Thank you very much! That was exactly what I needed!

_______________________________________________
nginx mailing list
nginx@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx
Sorry, only registered users may post in this forum.

Click here to login