I am really a noob when it comes to PHP and I would like to ask for your help. I would like to create a 404.php for my Wordpress site that shows the 404 page in the appropriate language. The site is multilingual: Dutch and English. (I tried some plugins but they don't work with my current theme). I have done some research and found some bits and pieces and mastered a piece of php code that doesn't work. If you could be so kind to point me in the right direction, that would be great. This is what I came up with:
<?php
$incomingUrl = $_SERVER['REQUEST_URI'];
if($incomingUrl == 'damcms.com/en') {
wp_redirect(damcms.com/en/page-not-found-404);
exit;
} else {
wp_redirect(damcms.com/pagina-niet-gevonden-404);
exit;
}
?>
I don't get an error message, nothing is being shown. So I miss something, what?Thanks in advance. Claudia
First, the reason nothing happens is here:
wp_redirect(damcms.com/en/page-not-found-404);
Any time you have a string in PHP it must be in quotes; otherwise PHP tries to interpret it as instructions (and damcms.com/en/page-not-found-404
is not a valid PHP instruction).
So this would work:
wp_redirect('damcms.com/en/page-not-found-404');
But there's a bigger issue: you don't want to just check if the URL is exactly damcms.com/en
— you want to check if it starts with damcms.com/en
.
So, perhaps this:
if(strpos($_SERVER['REQUEST_URI'], 'damcms.com/en') !== false)) {
wp_redirect('damcms.com/en/page-not-found-404');
} else {
wp_redirect('damcms.com/pagina-niet-gevonden-404');
}
exit;
But going one level deeper, even this is a little iffy. WordPress already has a 404.php
template that shows for any 404 page. I strongly recommend editing that — and there do the if ... 'damcms.com/en'
test and then perhaps just include a 404-en.php
template. That way the existing 404 mechanism still works. With the setup you have, the visitor is being redirected to a 200
page, so to a search engine it will appear your site has no 404s at all and everything is valid content!