2016-07-25 121 views
1

我有两种语言的网站,英语和荷兰语。在PHP中构建多语言(多语言)网站的自定义404页面

英语版本是在根目录(的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

回答

2

可以在.htaccess使用:

ErrorDocument 404 /error_en.html 

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

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

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

\x2F正则表达式,而不是/字符,你不能在正则表达式中逃脱内使用。

+0

不起作用。当我在/ nl /目录外键入一个错误的url时,它显示error_nl.html版本。它总是显示nl版本。 – Mickel

+0

你的'.htaccess'文件是否包含更多行?你能把它的内容放在一些pastebin中吗?你使用哪个版本的Apache? <?的PHP的phpinfo()>的溶液中的Apache 2.4.9 – Dekel

+0

进行测试时我运行它显示以下信息: 阿帕奇1.3(apache_hooks)\t 阿帕奇1.3 \t Apache 2.0的过滤\t Apache 2.0的处理程序(基于在Apache 2.0筛选器代码上) – Mickel

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文件不应该在你的服务器存在的,相反它会被显示给用户。