htaccess:2个不同的404错误文件(或内容)

I am using a .htaccess file to catch ErrorDocument. Now, I have :

ErrorDocument 404 /errors/error404.php

It works great, but now, I want another error 404 file (or another content for this file), if the previous url contained "eu".

ErrorDocument 404 /errors/error404.php
<!-- If URL contains "eu", do : -->
ErrorDocument 404 /errors/eu_error404.php

Is this possible ? Or can I catch the URL where I came to execute differents include with PHP ?

I would like to avoid $_SESSION..
I saw things like : $_SERVER['HTTP_REFERER']

Can someone explains me :) ?

Save your code and try the following code :

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (eu)+
#the line above will catch any request contains eu
RewriteRule ^(.*)$ /errors/eu_error404.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(eu)+
#the line above will exclude any request contains eu
RewriteRule ^(.*)$ /errors/error404.php

Update

If you wanna only the request that start with eu change the code as follow :

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/eu
#the line above will catch any request starts with eu
RewriteRule ^(.*)$ /errors/eu_error404.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/eu
#the line above will exclude any request starts with eu
RewriteRule ^(.*)$ /errors/error404.php

And regarding your question is that affect the SEO , it is not SEO issue but user experience issue , for example if you have old pages and users still requested and you redirect them for irrelevant page that will give them bad so the best way to handle it with alternative pages , regarding the search engines the 404 when it comes from any request will be on hold then removed after many request but more 404 is not good in some situation so , it depends in your expected error pages , are they already indexed before and it is better to redirect them for related pages or just wrong request and you wanna guide users to right way but believe the wrong request will not be more than correct in normal practice so it is not issue to redirect them to page that explain their mistake and guide them to correct page etc..

Unless you have access to the server config (or are using Apache 2.4+), it may be easier to test the URL inside the error document file and generate the appropriate content in PHP.

However, you should be checking against $_SERVER['REQUEST_URI'] or $_SERVER['REDIRECT_URL'] (which excludes the query string, if any). Note the difference between _URI and _URL. Note also, that $_SERVER['REDIRECT_URL'] only works inside an error document.

The $_SERVER['HTTP_REFERER'] contains the value of the HTTP Referer header (if any). ie. the URL from which the request is being made. This is sometimes empty and for a direct request it will indeed be empty.

Testing simply for "eu" anywhere in the URL does seem a bit too general? (Does the URL start with /eu/, as implied by your comment?) But anyway, you know your URLs.

For example:

<?php
$htm = '';

// strstr() is case-sensitive
$isEu = strstr($_SERVER['REDIRECT_URL'],'eu');
if ($isEu) {
    $htm .= 'EU Error document....';
} else {
    $htm .= 'Error document for everyone else...';
}
echo $htm;

Using the referer dose not make much sense in my opinion, because 404 Error pages are meant for an URL that somebody is trying to reach but do not exist for what ever reason. What you can do is the flowing: Set your default Error Document as usual

ErrorDocument 404 /errors/error404.php

Now check if the requested URL (you also can use %{HTTP_REFERER} if that rally makes sense to you) starts with 'eu' if so use the different Error Document

<If "'%{REQUEST_URI}' =~ m#^/?eu#">
   ErrorDocument 404 /errors/eu_error404.php
</If>

So for example.com/page-do-not-exit => default 404 page but for example.com/eu-page-that-do-not-exist => we get the eu 404 version.

You need apache 2.4 for this! https://httpd.apache.org/docs/2.4/de/mod/core.html#if

Doing something in your PHP is not necessary, but if you want you can do it of course.

Just checking your $_SERVER['REQUEST_URI'] or $_SERVER['REDIRECT_URL'] Variable