Caddy: Redirect www subdomain to main domain AND remove the trailing slashes - redirect

The bounty expires in 3 days. Answers to this question are eligible for a +100 reputation bounty.
beta-developper is looking for an answer from a reputable source.
I am trying to redirect all www subdomain urls to the main domain.
The basic configuration is this one and it works fine:
https://www.{$HOST} {
redir https://{$HOST}{uri} permanent
}
This works just fine
Example:
https://www.my_domain.ltd/path/to/url redirects to https://my_domain.ltd/path/to/url with a 301 code.
With that, I also want to get rid of the trailing slashes if they exists
Example:
https://www.my_domain.ltd/path/to/url/ have to get redirected to https://my_domain.ltd/path/to/url and not https://my_domain.ltd/path/to/url/
Is there a way to do that using caddy's configuration?
I try this, but I got a parsing error:
https://www.{$HOST} {
rewrite {
to {path} {path}/ /{path}
}
redirect https://{$HOST}{uri} permanent
}
The error: Error during parsing: Wrong argument count or unexpected line ending after 'rewrite'

Related

haproxy rewrite on backend to add a path for some requests to a specific domain

I am looking to try and get haproxy to rewrite a url on the backend. For example if the end user navigates to bitechomp.domain.com they should only see this URL but the request to the backend server needs to have the request re-written to include a path. e.g. bitechomp.domain.com/bitechomp
I believe I have the regex to match it, but struggling to find the syntax to then just have it add the folder path at the end.
^([a-zA-Z0-9]/)?(bitechomp).$
I believe I have resolved this.
http-request set-path /bitechomp/ if { path_reg ^([a-zA-Z0-9]/)?(bitechomp).$ }
This works for any domain so both bitechomp.domain1.com and bitechomp.domain2.com would be re-written to bitechomp.domain1.com/bitechomp and bitechomp.domain2.com/bitechomp

Nginx redirection for entire subfolder with screening for special characters

I have trouble making this work:
I need all links from
.domain.com/category/1
redirect to
*.domain.com/#something-{"[1]":"[]"}
.domain.com/category/2
redirect to
*.domain.com/#something-{"[2]":"[]"}
So I'm stuck on both making a regular expression and making proper screening for special characters:
So far did that:
server_name 1.domain.com 2.domain.com 3.domain.com;
location /^category/1$ {
return 301 /%23something-{"[1]":"[]"};
}
location /^category/2$ {
return 301 /%23something-{"[2]":"[]"};
}
And it doesn't work actually and takes some 10 lines, cause I have 10 categories.
And also I need to redirect /category/ itself to /
After endless tries I just installed a redirection plugin.

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

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

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.

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