Hi,
I want to redirect all urls like:
https://mydomain.com/xxx.jpg
to
https://anotherdomain.com/xxx.jpg
by regexp: ^.+\.jpg
Redirect and Redir seems like cannot do this. Is there other way to realize this?
Thanks.
Only rewrite supports regular expressions and other fancy rules. It seems that these features have been popular and no reports of slowness yet, so we may add it to redir eventually. What do you think @abiosoft? Can we do it without duplicating the code?
Sure, there should be a way without code repetition. I can have a look.
Abiola and I have discussed this a bit and we are willing to implement the functionality _if_ we can find a good, elegant way to do it. We're open to well-thought-out proposals for how this would look in the Caddyfile, preferably without breaking redir as it currently stands.
Adding to what Matt said, we have experimented with regexp and conditions in rewrite and we have gotten many positive feedbacks. With the number of requests for similar feature for redir, we are now willing to add the features to redir.
The challenge is supporting rewrite features (basically if and regexp) and ensuring it is backward compatible with the current redir syntax.
For example, how can redir support if and regexp and still support this syntax.
redir 307 {
/foo /info/foo
/todo /notes
/api-dev /api meta
}
How about:
Left column: If !strings.HasPrefix(…, "/") then is a regexp and must be in quotes.
Right column can have a scheme. Or start with // in which case the current request's scheme is prepended. If the left column is a regexp, this would accept match groups.
Non-regexp enjoy precedence (checked first) over regexp.
Related: #856.
I think this is now possible with redir's advanced features, like:
redir 307 {
if {path} ends_with .jpg
/ https://anotherdomain.com{uri}
}
(no regexp needed)
But what can I do in case my backend is changed when old URLs are still flowing on the internet?
In my particular case, I ran single language blog with URLs like: /posts/<title>/, but now blog became multilanguage, and URLs changed to /en/<title>/, /ru/<title>/ and so on.
Default language is Russian, so, for now, rewrite looks like:
rewrite /posts {
r (.*)
to /ru{1}
}
This allows me to serve links from the wild internet, but I have a problem with Google: it gets angry when /posts/pyat-samyh-poleznyh-statey-bloga-amplifera/ and /ru/pyat-samyh-poleznyh-statey-bloga-amplifera/ has the same content and I need 301 here.
Is there any way to do something like:
redir /posts {
r (.*)
to /ru{1}
}
or
rewrite /posts {
r (.*)
to /ru{1}
status 301
}
?
Thanks in advance.
@Bregor Thanks for your question -- we'll talk about it in the forum where you reposted it: https://forum.caddyserver.com/t/rewrite-with-redirect-or-redirect-with-regex/600
@mholt thank you.