Archived topic
redirect 404 to a gb created page - possible?
4 replies · Started by Matthias on May 17, 2020
Hi there,
i found a code that is said to make it possible to redirect 404page to home.
<?php
header("HTTP/1.1 301 Moved Permanently");
header("Location: ".get_bloginfo('url'));
exit();
?>
Which makes me wonder - and is my questions:
is it possible to redirect 404 page/errors to a specific page (lets say a page called "error")?
So you can create this page with gb - and have it a bit more shiny then the normal ones.
Edit:
just tried a little:
<?php
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://www.example.com/error");
exit();
?>
I created a 404.php in my child theme directory and added the text - and opening a non-existing page on my website redirect to the url entered above.
So this seems to work. Do you see any (security) problems with that code? I just grabbed it from a tutorial - and can't code myself.
Hi there.
There is a WordPress Plugin that does exactly this. It doesn't require any coding, redirects, child theme templates, or additional server requests.
Just create a page and assign it as your custom 404 error page. Works very well.
Hope this helps!
well - there is probably a plugin for everything. But personally i prefer a little code over that as in my experience a lot of plugins aren't supported in the long run. I currently have to recreate a whole page because of this reason.
so - if it is as easy as creating a small 404.php with a little code in it - i'd prefer this a lot over.
Also - it seems the plugin doesn't use gutenberg - while with the redirect code you can simple create a page with gutenberg.
But still - thx for your post - it's always good to have options.
So the question still is if there may be any problems with that code when its used with gp? Or if there is a better solution for this theme.
Hi there,
That plugin is what we usually suggest actually.
GP doesn't do anything special for 404 page so if the code works for you then I don't see any issues.
Can't say about security though - this is something you'd need to check with the code author
thx for the inport leo.
I actually just found that there is also i wp function for that.
So i replaced the former code with the customized example from wordpress.org.
<?php wp_redirect( 'http://www.example.com', 301 ); exit; ?>
Result is the same - but guess if there is a wp function i might as well use it.