.htaccess 如何在htaccess中编写ErrorDocument命令依赖于语言URL?

56lgkhnf  于 2022-11-16  发布在  其他
关注(0)|答案(1)|浏览(96)

在一个多语言网站,我需要为每种语言设置一个自定义404页面。我想从URL获得语言。
www.../lang/page/
如何在.htaccess中编写ErrorDocument命令,使其依赖于语言URL?

ErrorDocument 404 /404/
um6iljoc

um6iljoc1#

假设语言代码是所请求URL路径的第一个路径段中的两个字符的小写字符串。如果不存在,则默认为en
请尝试以下操作:

# Get 2 char language code from URL (default to "en") and assign to LANG env var
SetEnvIf ^ ^ LANG=en
SetEnvIf Request_URI "^/([a-z]{2})/" LANG=$1

# Set ErrorDocument dynamically using the language code
# Requires Apache 2.4.13+
# The 2nd argument to the ErrorDocument directive must be prefixed with a slash
# in the source code itself for it to be interpreted as a local path.
ErrorDocument 404 /%{reqenv:LANG}/404/

使用表达式语法生成动态ErrorDocument字符串并获取环境变量的值需要Apache 2.4.13+。
参考编号:

  • https://httpd.apache.org/docs/current/mod/core.html#errordocument

相关问题