I created a 404.php
page for a website. Also, there is the .htaccess
file ( in the /root ) having the ErrorDocument 404 /404.php
line.
The website is having a multi-language functionality, something like this:
sitename.com/it/article1
sitename.com/en/article1
and so on ... There are numerous articles.
The 404.php
page appears when I'm trying to access something like sitename.com/adsdasaerera
but it doesn't appear when I'm trying to access sitename.com/en/adsdasaerera
, adsdasaerera
not being, obviously, an existing article.
How can I achieve this?
Ideally you would have one error 404 document page. On that page you would first set a default locale of en, and then determine the users browser locale/language they are using, and proceed to the next step which is displaying the error page in that language.
Since we are talking PHP, here is an answer how to detect the locale in PHP. Simplest way to detect client locale in PHP
From there, the next step is basically using the strings associated for that locale from an array or a file and displaying them.
On this page you could also have some links to other translations that could be viewed(hidden elements that show div on click), if the user doesn't wish to read the error in the language their browser is set to use.
您是否尝试过提供完整路径?
ex ErrorDocument 404 /site/error/404.html
Have you tried put the line ErrorDocument 404 /404.php
into the apache global config file, maybe in /etc/httpd/. Or is there an exising .htaccess file under somesite.com/en/ folder?
how do you archieve the multi-language functionality? rewrite in apache or .htaccess? and what does your PHP code?
in addition to Branimir Đureks answer, you may analyze the $_SERVER['REQUEST_URI']
string ie using explode()
and if the article dosn't exists in the choosen language, send header("HTTP/1.0 404 Not Found");
otherwise display the article.
If you are NOT using a CMS etc, so the Language Folders are really existing, you can use this:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(ie|en|de)/ /404.php/$1 [R=404,L]
Explanation
/404.php?lang=$1
, eg /404.php?lang=en
with a 404 code In that 404.php you just need to Use $_GET['lang']
to get the requested Langugage.
But you already said, that the multi-language functionality is archieved in .htaccess . It would be helpfull to know whats in that file.
Anyway: If all Requests get redirected to one page (eg index.php), somethere in that file the Content of the site gets included. That hapens either with an include or similar of a file or with a Query to the Database. Thats the point there you need to expand your code. If that file isn't found or if there is no record in the Database, you need to include your 404 File.
Please use this code at the bottom of .htaccess. Replace Path with your path.
RewriteRule ^pagenotfound$ 404.html ErrorDocument 404 path/pagenotfound
It should be possible to define an ErrorDocument
like this in htaccess
file:
ErrorDocument 404 http://sitename.com/404.php
and then do a language specific redirect check for requests in 404.php
file.
It doesn't redirect you to the 404.php file because you accessed the existing file. Look at this URL
sitename.com/en/adsdasaerera
"adsdasaerera" is probably only a parameter value that is rewrited by htaccess. It's not a file. I suppose you have a "en" folder and index.php in it. So when you enter url above, you access that index.php in "en" folder with parameter "adsdasaerera" and that's the reason why you don't get 404 error. You can solve it by adding little code that searches trough the database for "adsdasaerera" and if it doesn't exist, in your code manually redirect it to 404.php file.
The reason why you get 404.php on this url
sitename.com/adsdasaerera
is because you don't have file named "adsdasaerera".
Hope it will help you :)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.