2016-07-25 90 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文件不應該在你的服務器存在的,相反它會被顯示給用戶。