用 PHP 构建的多语言(多语言)网站的自定义 404 页面

Mic*_*kel 1 php apache .htaccess multilingual

我有两种语言的网站,英语和荷兰语。

英文版在根 (public_html) 中,荷兰版在 /nl/ 目录中。

我的网站是用 PHP 构建的。

当链接断开(页面移动或不再存在)时,我希望讲荷兰语的人(荷兰人和比利时人)被重定向到荷兰语的 404 页面,其余的重定向到英语的 404 页面。

如何为此多语言(多语言)网站创建自定义 404 错误页面?请给我一个完整的代码,例如(php 和/或 .htaccess)。

我的 .htacces 文件包含以下几行:

# Instructs webbrowsers how to cache content
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access 1 year"
ExpiresByType image/jpeg "access 1 year"
ExpiresByType image/gif "access 1 year"
ExpiresByType image/png "access 1 year"
ExpiresByType text/css "access 1 month"
ExpiresByType text/html "access 5 minutes"
ExpiresByType application/pdf "access 1 month"
ExpiresByType text/x-javascript "access 1 month"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresByType image/x-icon "access 1 year"
ExpiresDefault "access 1 month"
</IfModule>

# Rewrite non-www to www
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

RewriteEngine on
RewriteRule ^(.*)\.html$ $1.php [nc]

# Error Page(s)
ErrorDocument 404 /404.php
Run Code Online (Sandbox Code Playgroud)

Dek*_*kel 5

你可以在.htaccess

ErrorDocument 404 /error_en.html

<If "%{REQUEST_URI} =~ /nl\x2F.*/">
    ErrorDocument 404 /error_nl.html
</If>
Run Code Online (Sandbox Code Playgroud)

默认错误页面将是error_en.html,如果有人尝试访问/nl/目录中不存在的页面- 他将获得error_nl.html页面的内容。

确保error_en.html和error_nl.html文件存在于您的目录下。

请注意在\x2F正则表达式中使用inside 而不是在正则表达式/中无法转义的字符。