Rewrite a url containing percent signs with nginx - nginx

My site needs to take what is essentially a malformed url sent by another site:
http://mysite/%SAMPLE_URL_STRING%
and redirect it to a proper url using nginx. None of the rewrite rules I've tried seem to catch it, instead always returning a 400.
As the single percent sign, not describing an encoded character, is itself an unsafe character as described in the RFC (http://www.ietf.org/rfc/rfc1738.txt) I suppose it might be an insurmountable issue.
However, sample (non-functional) rewrite rules include:
rewrite /%SAMPLE_URL_STRING% /redirect.html permanent;
rewrite ^/\%SAMPLE_URL_STRING\%$ /redirect.html permanent;
rewrite /\%.*\% /redirect.html permanent;
Even venturing into UTF8:
rewrite (*UTF8)^\x25.*\x25$ /redirect.html permanent;
Is it possible that any url containing %SAMPLE_URL_STRING% cannot be redirected by nginx?

Related

nginx rewrite preserving CGI query params (with a hash anchor)

For my www.example.com nginx config, I have these rewrite rules:
rewrite ^/foo$ https://one.example.com/page#one permanent;
rewrite ^/foo(\?.*)$ https://two.example.com/page$1#two permanent;
rewrite ^/bar$ https://three.example.com/page#one permanent;
rewrite ^/bar\?(.*)$ https://four.example.com/page?$1#two permanent;
A request for http://www.example.com/foo correctly redirects to https://one.example.com/page#one.
A request for http://www.example.com/bar correctly redirects to https://three.example.com/page#one.
A request for http://www.example.com/foo?extra=yes incorrectly redirects to https://one.example.com/page#one?extra=yes (I expect it to go to https://two.example.com/page?extra=yes#two).
A request for http://www.example.com/bar?extra=yes incorrectly redirects to https://three.example.com/page#one?extra=yes (I expect it to go to https://four.example.com/page?extra=yes#two).
How can I redirect to a page copying CGI parameters and linking to a particular anchor in the destination page?
It appears that the rewrite directive does not handle the # fragment correctly when assembling the query string into the replacement string.
You can prevent rewrite from appending the query string by adding a trailing ? to the replacement string. So, you can construct the correct result using the built-in variables $is_args and $args.
For example:
rewrite ^/foo$ https://one.example.com/page$is_args$args#one? permanent;
See this document for details.
Note that the query string is not part of the normalised URI used to match rewrite and location statements, so your ^/foo(\?.*)$ regular expression will not work.

nginx rewrite all trailing / to /index.html with proxy pass

Using nginx as a reverse proxy, I'd like to mimic the index directive with proxy_pass. Therefore I'd like nginx to query /index.html instead of /, /sub/index.html instead of /sub/.
What would be the best approach to do this ?
Not sure if it's relevant, but the proxied server does answer HTTP 200 on /, but I'd still like to rewrite it to /index.html.
As the / request leaks some information by listing the directory content, I'd also like to be sure that no one will be capable of accessing it (like doing something like /sub/..).
Thanksies
Just add :
rewrite (.*)/$ $1/index.html last;
rewrite (.*)/..$ $1/../index.html last;
Should works

How to avoid "too many redirects" with NGINX

Here is the rewrite I added and when I visit /name I have an infinite loop of redirects.
rewrite ^/name /name-and-more permanent;
What am I missing?
You rewrite ^/name to /name-and-more which results in a 301 redirect.
Then next request is for /name-and-more which matches the same rewrite ^/name, so it redirects again to /name-and-more, and again etc..
You have missed the trailing $ so that it matches exact URL /name.
So:
rewrite ^/name$ /name-and-more permanent;

nginx rewrite middle of url to another pattern

This nginx rule works great for me for a full specified file path
rewrite ^/sitemap.xml$ /sitemap.php last;
When I acces sitemap.xml it works as expected but in the background sitemap.php is requested. So far so goode.
Another problem arised and I need to rewrite the last part of existing urls
rewrite ^doctor-solution.html/ doctor-answer.html/ permanent;
What I want to achive is when an old url like
https://example.com/case12232-doctor-solution.html/ is accessed
it must be redirected to
https://example.com/case12232-doctor-answer.html/
But My rule doesn't seem to work. Any ideas?

Nginx replace // with / in URL

We are currently getting web requests with www.domain.com//index.php which are resolving, but causing issues with google. How can we rewrite the request to catch these and redirect to www.domain.com/index.php
Thanks
By default ngingx merges double-slashes and urls like www.domain.com//index.php works well.
You can turn off merging and make rewrite rule with redirection:
merge_slashes off;
rewrite (.*)//(.*) $1/$2 permanent;

Resources