On many my sites there is a tradition of making redirects through an internal directory. It looks so that the external link to Yandex from site.ru looks like this:
https://site.ru/out/https://ya.ru
This url will 301 redirect to https://ya.ru.
In apache i do this in .htaccess:
RedirectMatch 301 /out/(.*)$ 1.
Trying to rewrite for nginx, doing that:
rewrite /out/(.*) $1 permanent;
But it redirecting to https://site.ru/ya.ru, not to https://ya.ru.
I add this config to my.conf (i created this file in /etc/nginx) and do «include /etc/nginx/my.conf;» (in server{} of course) in all conf files of my sites.
Help me please with the config, I can't figure it out.(
Part of the problem is that rewrite uses a normalised URI which has folded multiple consecutive /s into a single /. It is better to use the original request which is stored in $request_uri.
One possible solution is to use a map to extract the embedded URL. For example:
map $request_uri $redirect {
default 0;
~^/out/(?<out>.*)$ $out;
}
server {
...
if ($redirect) { return 301 $redirect; }
...
}
See this document for details, and this caution on the use of if.
You can achieve the same thing without using a map using:
if ($request_uri ~ ^/out/(.*)$) { return 301 $1; }