Redirects on language etc, suburls to different rootdir

Hi,
I’m converting Open Source project website to caddy, and have these problems:

  1. example.com root should redirect to /fi/ Finnish language if it’s in Finnish, otherwise to English /en/ . I tried language plugin but it had only rewrite example. There is no PHP on that server.

Original Apache rule is:

    # Automatically redirect requests on / according to the
    # "Accept-Language" HTTP header, if a translation is
    # available for that language.
    RewriteEngine On
    RewriteCond %{HTTP:Accept-Language} (\w\w)
    RewriteCond %{DOCUMENT_ROOT}/%1 -d
    RewriteRule ^/$ /%1/ [QSA,R=302,L]

    # If not, go to English.
    RewriteCond %{HTTP:Accept-Language} (\w\w)
    RewriteCond %{DOCUMENT_ROOT}/%1 !-d
    RewriteRule ^/$ /en/ [QSA,R=302,L]

I did try this but it did not work:

    locale en fi {
      detect header
    }
    redir {
      ext /
      to /{>Detected-Locale}/
    }
    header / Vary "Accept-Language"
  1. How do I get this redirect group working? In old URL it is /topics/{1}/posts/{2} and it should redirect to /posts/{2} . It could also have {2} changed to {url_escaped}.

    redir 301 {
    /topics/(.)/posts/(.) /posts/{2}
    }

  2. How do I configure suburl to have html files in different directory?

Original Apache rule is:

Alias /docs /srv/http/docs

Maybe something like this?

example.com {
    root /var/www/dir1
    ....
   example.com/docs {
         root /another/dir/elsewhere
   }
}

I did that redirect with javascript meanwhile.

<script type="text/javascript">
        var userLang = navigator.language || navigator.userLanguage;
        if (userLang == "fi-FI") {
            window.location.href = "https://example.com/fi/";
        }
        else {
            window.location.href = "https://example.com/en/";
        };
</script>
  1. redir doesn’t use the same syntax as rewrite - it reads this line:

as “redirect from /to to /en/”, for example - see the redir docs. Try something like this:

redir {
    / /{>Detected-Locale}/
}
  1. Unfortunately regex isn’t available for the redir directive which makes it difficult to achieve this /topics/{1}/posts/{2} to /posts/{2}. You could rewrite it instead, which looks something like:
rewrite /topics {
    r ^/(\w+)/posts/(\w+)/?$
    to /posts/{2}/
}
  1. You can define an entirely separate vhost, specific to the /docs directory. You will need to copy across all the directives you’d like it to have in common with the main vhost, modifying root as necessary.
example.com {
    root /var/www/dir1
    ...
}
example.com/docs {
    root /another/dir/elsewhere
    ...
}
  1. didn’t work for me, I would need something similar as in this Javascript logic. I tried to make that logic working with redir docs but with no success yet.

Logically, they are the same. {>Detected-Locale} is “fi” when detected, “en” if otherwise, because you set “en” as the first language to detect (which is therefore the default).