3

I have a question and couldn't find a solution for it.

We have a Wordpress page where the client wants multiple error pages.

1 error page is in the usual normal 404.php for the custom theme which we created. Now I want to add multiple error pages with different styling and html structure.

example:

localhost/blog/{post-name} -> if post-name is not found load me the post-404-error.php

localhost/service/{service-name} -> if service-name was not found load the service-404-error.php

I couldn't think of a good solution how to get it done the right and save way.

I thought about changing the .htaccess like described here: https://stackoverflow.com/questions/24198493/404-redirection-to-multiple-error-page-htaccess

But if permalinks are saved the .htaccess is rewritten and my changes will be gone there. Also I didn't know how to put the correct error documents inside (my rewriting htaccess knowledge is not the best.) I thought there could be a coded way with php/js something?

I also thought about some complicated check if this url is called and then check if is_404() but I guess its too complicated.

Thanks in advance for any help and tips :)

3
  • 1
    HTAccess has nothing to do with which template is loaded for the 404, HTAccess is responsible for telling Apache how to map HTTP requests on to WordPress so it knows to load WordPress when a user visits. You have zero direct control over which theme template is loaded from that file Commented Oct 3, 2022 at 14:27
  • 1
    Hi, as 404 is returned by the same regardless of the url, The best path, I would go with one template and, inside of it, try to identify the slug (or use regex to match the start of the url) and then display a custom block for each case you need. Commented Oct 3, 2022 at 14:53
  • "if permalinks are saved the .htaccess is rewritten and my changed will be gone" - no it won't. That would only happen if you manually edited the code inside the WP code block (which you should avoid). However, as noted above, this is not something you would do in .htaccess anyway (Apache's ErrorDocument is not going to help here). The question you've linked to on StackOverflow is not using WordPress (and the SO question / answer are ambiguous/incorrect for a number of reasons). Commented Oct 3, 2022 at 17:07

2 Answers 2

1

Thank you guys for the help and the tips. @Dexter0015 Tip helped me in the end to get it done.

This is how my 404.php looks like now im only checking if in the url the string is present and show the template for it.

Thanks everyone again

edit:

i changed the search values to /services/ and /blog/ so it was a bit better when the url has services inside or something it got messed up now it works perfectly for me

$postUrl = 'http' . (isset($_SERVER['HTTPS']) ? 's' : '') . '://' . "{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}";

get_header();



if (strpos($postUrl,'/services/') !== false) {
    get_template_part("error-pages/services-error-page");
} elseif(strpos($postUrl,'/blog/') !== false) {

    get_template_part("error-pages/blog-post-error");

}else{
    get_template_part("error-pages/general-404-error");
}




get_footer();
0

If your client wants only visual differences may be should consider using the get_template_part() function.

2
  • there are 2-3 different 404 pages , so litterely what you suggest if i understand this right is check the url and if there is_404() && {specific-url} then show the template ? and this inside the 404.php? Commented Oct 3, 2022 at 19:09
  • Yes. And you presented this implementation with your answer. Commented Oct 4, 2022 at 14:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.