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

时间:2016-07-25 13:42:16

标签: 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
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:4)

您可以在.htaccess

中使用此功能
ErrorDocument 404 /error_en.html

<If "%{REQUEST_URI} =~ /nl\x2F.*/">
    ErrorDocument 404 /error_nl.html
</If>

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

确保 root 目录中存在两个 error_en.html和error_nl.html文件。

  

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

答案 1 :(得分:0)

您可以重定向error.html以显示由PHP文件生成的页面,您可以生成多语言输出。

ErrorDocument 404 /error.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^error\.html$ error.php [NC,R]

但是error.html文件不应该存在于您的服务器中,而是会向用户显示。