Welcome! Log In Create A New Profile

Advanced

Help with location match regex for encoded / unencoded url

Posted by mitchellkrog 
Help with location match regex for encoded / unencoded url
August 09, 2018 04:53AM
Hi all, I really hope someone can assist me as I am out of ideas to get a regex working for this location match.

I need a location regex to match the following requests and redirect them to the correct location but regex has me totally stumped on this one.

The unencoded URL string is:

/historical-rainfall-trends-south-africa-1921–2015.pdf

The encoded URL string is:

/historical-rainfall-trends-south-africa-1921%C3%A2%E2%82%AC%E2%80%9C2015.pdf

How can I get a location match for these? It's literally only one pdf file that got uploaded with a messed up filename and now I am unable to redirect search engines to the correct file.



Edited 1 time(s). Last edit at 08/09/2018 04:53AM by mitchellkrog.
Re: Help with location match regex for encoded / unencoded url
August 09, 2018 07:06AM
I get a match with this regex here - https://regex101.com/r/3Lk2zr/3

but then using this

location ~ /.*[^\x00-\x7F]+.* {
return 444;
}

still gives me a 404 and not a 444

Likewise I get a match with this - https://regex101.com/r/80KWJ8/1
But then
location ~ /.*([^?]*)\%(.*)$ {
return 444;
}

Gives 404 and not 444 ???? I might just add my regex sucks



Edited 1 time(s). Last edit at 08/09/2018 07:09AM by mitchellkrog.
Re: Help with location match regex for encoded / unencoded url
August 09, 2018 08:05AM
SOLVED with this.

location /resources {
location ~* \.(jpg|jpeg|png|gif|ico|css|js|pdf)$ {
if ($request_uri ~ .*%.*) { return 301 https://example.com/resources/weather-documents/historical-rainfall-trends-south-africa_1921_2015.pdf; }
if ($request_uri ~ .*[^\x00-\x7F]+.*) { return 301 https://example.com/resources/weather-documents/historical-rainfall-trends-south-africa_1921_2015.pdf; }
}

See: https://stackoverflow.com/questions/51747175/nginx-location-match-regex-for-special-characters-and-encoded-url-characters
Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 94
Record Number of Users: 6 on February 13, 2018
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready