1

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

ThisIsJuke
  • 93
  • 12
  • Check out [this](https://serverfault.com/a/314538) answer. Instead of %{ENV:device:.iphone} use %{HTTP_REFERER}. – brevis Oct 02 '17 at 14:32
  • It seems to be what I want, thanks ! But, in my 404 error file, when i do : `echo $_SERVER['HTTP_REFERER']`, it's echoing nothing. What i am doing wrong ? Was on myurl.fr/eu/ going to --> myurl.fr/eu/blabla (404 page) – ThisIsJuke Oct 02 '17 at 14:43

3 Answers3

2

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

Webdesigner
  • 1,954
  • 1
  • 9
  • 16
1

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;
MrWhite
  • 43,179
  • 8
  • 60
  • 84
  • 1
    It works, thanks for this ! Upvoted, I gonna try the solution without PHP, and if the result is not as good, it will be my solution :) – ThisIsJuke Oct 02 '17 at 15:25
0

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

Mohammed Elhag
  • 4,272
  • 1
  • 10
  • 18
  • Ok, just perfect, working good with just copy and past ! This is my solution, no need to update my VM ! Thanks for this ! – ThisIsJuke Oct 02 '17 at 16:39
  • glad to help , wish you all the best – Mohammed Elhag Oct 02 '17 at 16:39
  • 1
    Sorry but this code have a big issue.... Yes you will see the Error page but the Server is not sending the 404 status Code... so for Google and Co. all the pages that actually do not exist, it looks like as they are normal pages. – Webdesigner Oct 02 '17 at 17:07
  • 2
    I'm not sure if this should/must also work with some kind of CMS or PHP Framework, but if so than all URL including the word 'eu' will be redirected to the 404 Page. Here a list of English word you are not allowed to use in your URL https://www.morewords.com/contains/eu/ – Webdesigner Oct 02 '17 at 17:12
  • it depends on pages returning 404 codes are high-authority pages or not and so many factors according to website status with error pages if it is big amount or not and so on – Mohammed Elhag Oct 02 '17 at 18:06
  • Hi guys, i changed the regex fo course, in reality the word can't begin with "/eu" (and it's what I want). There will be a SEO problem ? Or I can use this code ? – ThisIsJuke Oct 03 '17 at 08:26
  • if you want just not begin with eu , i will change , but as you asked not contained this – Mohammed Elhag Oct 03 '17 at 14:00
  • You sill do not get real 404 page just a regular page (Status code 200) that looks like a 404 page!!! https://stackoverflow.com/questions/4423061/view-http-headers-in-google-chrome If you get Status code 404 than every thing is OK if not than this answer do not work for you. – Webdesigner Oct 03 '17 at 16:50