How to make a redirect to nginx, depending on the part of the URL? - redirect

Welcome essence of the problem.
We have a path
site.com/seveniry-dlya-turistov/...
(Where ... is the character code specific record (of goods), and in front of it, as you might guess - section of this article)
Those show on site.com website under "souvenirs for tourists" page of a souvenir.
I need to redirect this type:
If there is a request to .../eveniry-dlya-turistov/.. substitute the section title in the name of .../seveniry/..., see if there is a request for
site.com/seveniry-dlya-turistov/elemnet1/
we have to do a 301 redirect to
site.com/seveniry/elemnet1/
Please tell me how to do it, and why does not work like that ...
location /catalog {
rewrite ^/catalog/souvernirs-for-tourists/(.*)$ http://SITE-NAME/suveniry/$1 permanent;
}
Thanks in advance for your help!

Should be something like
rewrite ^/seveniry-dlya-turistov/(.*)$ /seveniry/$1 last;
But you need to test the exact regex since it's hard to do by head without testing. nginx will look for another location block matching /seveniry/$1 to serve content.

Related

HA Proxy rewrite and redirect

Trying to do a rewrite and redirect. I've been trying this, it works to some extent but not 100% what I want it to do
acl old url_beg /site/ab
http-request redirect location /new/%[query] if old
the url can be for example
https://host/site/ab/xx
https://host/site/ab/yyyy
https://host/site/ab/zzzzzz
https://host/site/ab/zzzzzz/asdajshdjasd
I am looking to grab the bold marked part and simply redirect the user to https//host/new/boldmarkedpart
Any string that comes after the bold marked part can be trashed. For example "/asdajshdjasd" in the last example.
Any idea how to accomplish this? Thank you!!
If i understand correctly, you want to split the path part of url and get its 4th part.
In string foo1/foo2/foo3/foo4/foo5 you want only foo4.
This should work for you:
acl old path_beg /site/ab
http-request redirect location /new/%[path,field(4,/)] if old
It may be confusing that you want 3rd directory from path and here you take 4th word, but that's because when you split /foo2/foo3/foo4/foo5 by / then the first word is empty.
field converter is documented here: https://cbonte.github.io/haproxy-dconv/2.2/configuration.html#7.3.1-field
Other notes:
%[query] would return the query part of url, which is everything after ? character and you don't have query part at all in your examples.
url in my tests had schema://hostname:port/path, so testing acl old url_beg /site/ab never matched, path is for that

Neatest way to redirect urls without trailing slash in HAproxy

What is the neatest way to redirect these
http://example.com/test to http://example.com/test/
(It should work no matter what "test" is). I'm new to HAProxy - I have different apache backends. Have it working for URLs of this format http://example.com/test/ but cannot get the other type to work.
Have tried:
(1)
http-request set-path %[path]/ unless { path_end / }
which doesn't load the html page properly - possibly because it's screwing up e.g. the referenced JS files.
(2)
http-request redirect code 301 prefix / drop-query append-slash if missing_slash
from the documentation. I figure I need some kind of slight adjustment to this but don't know where to start with it. Any hints would be much appreciated.
This would potentially best be done at the back-end, since only the back-end has a way to actually know which paths should be redirected. But, it should be possible from HAProxy.
It should work no matter what "test" is
I am skeptical that this is precisely what you need, because then (e.g.) /css/common.css becomes /css/common.css/, which would be wrong.
I'm inclined to think you want something like this:
http-request redirect code 301 prefix / drop-query append-slash if { path_reg /[^/\.]+$ }
Redirect to the same path, with the query removed and / appended to the end, if the path ends with a / followed by at least 1 (or more) characters that is neither / nor ..
This should redirect /test to /test/ and /hello/world to /hello/world/ but it should leave paths like /js/example.js and /images/cat.png and /favicon.ico unaffected.
Deeper nesting should work correctly, too, because regular expressions like this either find a match or don't, and this expression only considers whatever is after the final slash in the path. If it contains a dot or nothing, then there is no match, which seems correct.

Redirect ALL get parameters to another domain in Nginx

Only answers I can find for this have specific parameters and don't work if the parameters change.
I have a URL coming in such as:
/logs.php?user_id=10032&account_id=10099&message=0&interval=1&online=1&rsid=65374826&action=update&ids=827,9210,82930&session_id=1211546313-1602275138
I need to redirect this url to a completely different domain and file but keep the get params.
So it ends up redirecting to:
https://example.com/the_logger.php?REPEAT_ALL_GET_PARAMETERS_HERE
Here is what I have unsuccessfully tried so far:
location logs.php
{
rewrite https://example.com/the_logger.php?$args last;
}
But it doesn't seem to match the url or redirect. I think I'm misunderstanding the logic of nginx confs. If it were .htaccess I think I'd be okay. I can put a few more examples here if need be of what I'm trying to achieve.
As you state this is a redirect and not reverse proxying a request, I would use the return directive to tell the client to do a 301 or 302. Using the return directive is the simpler and more recommend approach to redirecting a client.
Something like should do what you want:
location /logs {
return 302 https://example.com/the_logger.php$is_args$args;
}
Where $is_args would output a ? if and only if the query string is not empty, and $args is the query string itself

301 Redirect Directory Matching

I have been unable to get an answer on this one. Basically, I have a bunch of urls, that go like this:
www.mysite.com/gb/best-price/description.shtml
www.mysite.com/fr/best-price/description.shtml
www.mysite.com/jp/best-price/description.shtml
I need them to be redirected to their direct match pages, that go like this:
www.mysite.com/gb/best-price/index.shtml
www.mysite.com/fr/best-price/index.shtml
www.mysite.com/jp/best-price/index.shtml
I know I could do a 1-to-1 redirect, but truth is, I have hundreds of such urls.. how do I get the redirect to match to the destination urls, in bulk without doing a 1-to-1? For example, /gb/best-price/description.shtml to /gb/best-price/index.shtml
Basically, gb to gb, fr to fr, jp to jp. The only difference in the original and new urls are the html file names (original ends with description.shtml, new urls end with index.shtml).
Thank you,
Alex
You could tried:
location / {
rewrite ^/(.*)/best-price/description.shtml ^//1/best-price/index.shtml permanent;
I think it could help you. !!

How do I used the map feature in haproxy to build massive redirect tables 1.5

I think the subject covers it. I need to build a list of many 301 redirects within the same domain i.e. redirect /some_old_place to /some_new_place. We have lots of content the gets retired or replaced based on uri. The quote from the current home page of haproxy states maps may be used to "build massive redirect tables" but for the life of me I can figure it out. I am using version 1.5 and here are the lines of the config which I am currently am working on.
redirect location %[map(/opt/local/etc/haproxy/redirect.map), hdr(url)] code 301
Given the above config line I get
[ALERT] 099/205518 (27981) : parsing [/opt/local/etc/haproxy/haproxy.cfg:83] : error detected in frontend 'LoadBalancer' while parsing redirect rule : expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was 'hdr(url)]').
I have tried many variations of this but have not found any success. If anyone knows how to do this please let me know?
This was the solution I came up with. Thanks JStarcher for the tip about spaces.
http-request redirect location %[capture.req.uri,map(/etc/haproxy/redirect.map)] code 301 if { capture.req.uri,map(/etc/haproxy/redirect.map) -m found }
In the referenced file redirect.map I have a source uri on the left and a destination separated by a space on the right.
/some_old_page.html /some_new_page.html
I have over 2k lines in the file at this point and I have seen no noticeable slowdown.
You have to use the http-request redirect directive which supports log-format strings.
Another little tricky gotcha, you can't have a space after the comma.
Try this:
http-request redirect location %[hdr(host),map(/opt/local/etc/haproxy/redirect.map)] code 301