1

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?

Florin M.
  • 2,159
  • 4
  • 39
  • 97
  • 1
    http://stackoverflow.com/questions/24673194/multi-language-custom-404-htaccess-rules-causing-redirect-loop http://stackoverflow.com/questions/2061381/browser-language-based-404-pages-with-mod-rewrite-how-to – Nesim Razon Jan 23 '15 at 07:22
  • I do want the same 404.php to be displayed for all languages sections contained by the site. – Florin M. Jan 23 '15 at 09:20
  • Are you using a CMS or similar? The Backgorund is: Do the Folders `en/` & `it/` really exist or are they just Parameters for a Processing Page – j_s_stack Jan 28 '15 at 04:07

8 Answers8

2

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.

Community
  • 1
  • 1
ART GALLERY
  • 540
  • 2
  • 8
0

Did you try giving it a full path?

ex ErrorDocument 404 /site/error/404.html
Julian Camilleri
  • 2,975
  • 1
  • 25
  • 34
0

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?

JasonW
  • 453
  • 1
  • 9
  • 16
0

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 i.e. 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.

bohrsty
  • 406
  • 6
  • 17
  • the multi-language functionality is archieved in .htaccess. So, the analyze for $_SERVER['REQUEST_URI'] should be in the .htaccess file? – Florin M. Jan 26 '15 at 05:54
  • could you give an example of that .htaccess, please? i guess there will be something like `rewrite sitename.com/en/ to sitename.com/index.php?lang=en` in that index.php you can analyze $_SERVER['REQUEST_URI'] to find out if that article exists... or if you get the article name directly in the rewrite, you don't need REQUEST_URI, you could check it directly... – bohrsty Jan 26 '15 at 19:46
0

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

  • The three conditions check that the requested file or folder does not exist
  • The rule checks that the requested url starts with one of the three countries then a /, captureing the country code to Group 1
  • It redirects to /404.php?lang=$1, e.g. /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 (e.g. 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.

j_s_stack
  • 667
  • 1
  • 5
  • 18
0

Please use this code at the bottom of .htaccess. Replace Path with your path.

RewriteRule ^pagenotfound$ 404.html
ErrorDocument 404 path/pagenotfound
Demo Ashish
  • 110
  • 1
  • 9
-1

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 :)

Branimir Đurek
  • 632
  • 5
  • 13
  • Could you give me a little example for searching through database for the respective file and redirecting to 404.php? – Florin M. Jan 26 '15 at 10:08
  • Well, I need more information about how your website works, but I will give it a try. So if you do have index.php, in you "en" folder, that takes title from the URL parameter, which is in this case "adsdasaerera" and it is probably the article's title, then you can search for that title in your database. If you can't find it then redirect it to the 404.php. – Branimir Đurek Jan 26 '15 at 10:25
  • At first I wondered why you were down voted, because I thought you were right. But then you started about the `index.php` taking `adsdasaerera` as a param. Although you do quickly mention `rewrited by htaccess`, it's very unclear what is going on if you have little understanding of `.htaccess` and/or the way web requests are being handled. – Hugo Delsing Jan 27 '15 at 14:13
  • You hadn't provide enough information. If you had, problem would be probably solved right now. We only can guess where the problem is. Well I have created a multi language web application that had the same problem as yours and it works perfectly. I work everyday with .htaccess and I understand perfectly how web requests are handled. At least I solved every problem that I had encountered. – Branimir Đurek Jan 28 '15 at 09:50
-1

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.

Debug Diva
  • 26,058
  • 13
  • 70
  • 123