Auto Website Language Switching with Caddy
Auto Website Language Switching with Caddy
I implemented an automatic language switching feature for my blog using Caddy. At first, I thought it would be simple, but I kept running into obstacles.
- It must determine whether it is the first visit; otherwise, if a user manually switches the language, Caddy cannot distinguish that type of request and will redirect indiscriminately.
- Then, it needs to check if the user’s language matches the page language; if they match, no switching should occur.
- Switching must only apply to HTML files (
not path_regexp \.[a-zA-Z0-9]+$); otherwise, requesting other types of files will result in “Too Many Redirects.” Furthermore, Hugo’s multilingual setup only generates HTML files and no other files. - For English scenarios, the
/zh-cnprefix needs to be removed.
Caddyfile
| |
Last updated on