如何在htaccess中编写错误记录命令取决于语言URL?

发布于 2025-02-10 13:20:01 字数 216 浏览 0 评论 0原文

在一个多语言网站中,我需要为每种语言设置自定义404页。我想从URL获取语言。

www .../lang/page/

如何在.htaccess中编写errordocument命令,以便取决于语言URL?

ErrorDocument 404 /404/

In a multi language website, where I need to set up a custom 404 page for each language. I want to get language from URL.

www.../lang/page/

How to write ErrorDocument command in .htaccess so that it depends on the language URL?

ErrorDocument 404 /404/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

無處可尋 2025-02-17 13:20:01

假设语言代码是请求的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+。

参考:

Assuming the language code is a two character lowercase string in the first path segment of the requested URL-path. If absent, this defaults to en.

Try the following:

# 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/

Using expression syntax to generate a dynamic ErrorDocument string and get the value of the environment variable requires Apache 2.4.13+.

Reference:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文