WordPress 404 Redirect And 404 Custom Message

The WordPress 404 error page or the 404 error message is probably the file or piece of code most neglected by WordPress theme authors. This short WordPress guide will show you how to spruce yours up a bit.

Keeping visitors on your WordPress blog should be one of your main objectives to blogging. When they find themselves on a dead end they may just leave. Why not entice them to look around a bit, redirect them to a different page, or place some AdSense on your 404 page and at least get paid when they leave your site.

First check yours out by typing in URL on your blog that you know doesn’t exist. Most WordPress blogs show something like this:

404.jpg

Well that is really wanting me to do something like… leave! WordPress handles 404 errors differently than an HTML site and creating a 404 redirect in your .htaccess file will not work, at least that I know of. The 404 error that is displayed is theme specific and has to be edited within the themes files. So switching themes means these changes will not follow.

Next determine if you have a separate 404.php file with your theme or if it uses some code in the themes main index file (index.php). From within the WordPress dashboard click Presentation then Theme Editor. Look on the right side for the 404.php file. If you have a 404.php file you can make the changes to it. If you don’t have a separate 404.php file you can create one or make the changes you want in the index.php file.

If you have a current 404.php file and want to redirect visitors to another URL. Or to make your own 404.php file to redirect your visitors to another URL place the following code into a text file, change the URL and save it as 404.php and upload it to your current WordPress themes directory:

<?php
header(“Status: 301 Moved Permanently”);
header(“Location:http://www.ChangeToTheURL.com”);
?>

Do not copy the code from the post! Click here to open the proper text file to copy.

Now if you want to display something more interesting on your 404 error page you can edit your 404.php file or find where in your index.php file to make the changes. To determine where the code is in the index.php file open it in the Theme Editor and look for this piece of code:

<?php else : //if there were no posts available ?>

You can now edit what you want between that bit of code in your index.php file and the next line of code. You can start by changing the text between the h2 tags shown below:

<h2>Error 404 – Not Found</h2>

You can also add a few links and more text below the h2 tags and even some AdSense code if you want. I added a search box for my blog and some AdSense. Make sure you backup any file you are editing first so if you don’t like your changes or break your blog you can restore it easily.

Here is a sample of what I used to create the 404 error message on this site: You can use this in either your index.php or 404.php file: Sample 404 Error You can add more space between items with a few more breaks <br/> if you like.

Well what started out as short guide has grown bigger than I thought and there are many other items you can place in the 404 error area of your blog. Just make sure you close all the tags and make a backup before you start hacking the code!

Similar Posts:



Tags: , ,

If you liked this post, feel free to leave a comment that is relevant to the post or subscribe to the feed and get future articles delivered to your feed reader.

Sorry but due to the spamming of a few all comments are moderated and will appear when approved and all drive-by comments will be deleted!

Comments

thanks for the tip. I’ve been wanting this, but I hope i will able to do this during weekends.

Thanks once again.

Reply

Thank you for sharing your thoughts. I will follow your example and redirect my visitors properly. I didn’t know how to do this in WP.

Best wishes

Reply

does this work on versions 2.5?…cause i did this and it shows an error…

The requested URL /asdasd/ad/a/dasda/ was not found on this server.

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

thanks…

Reply

Estatiktik, your errors are either theme or hosting account related. Your theme may not have a 404.php file or your hosting account is overriding it. And without a link to your blog all I can do is guess.

Reply

thank you jeff…i found a solution…

all i did was go to the permalinks in the settings…i dont know why that worked?…but it did…maybe it generated an htaccess or updated it…

anyways,this is a very useful tip…thanks again…

Reply

Thank you so much for this info. I have been trying to find out how to do this for a long time. I couldn’t work out why the usual .htacces method didn’t work. Now I do thanks again.

Reply

Thanks for that. I’ve been meaning to get my 404 to redirect to my homepage for ages now.

Reply

Hi.. I wanted to use the 404.php file as above with the atahualpa theme but where ever i put the file it doesnt work.

My problem is that a lot of old pages are still in google (like http://www.testmybroadband.co.uk/?m=200902)on a clients blog, and i want to redirect to the home page.

This theme does not have a 404.php file to start with….

help?
thanks

Reply

Thanks for the 404 info! I was looking for this!

Reply

Thanks alot. I have had problems with 404 error and after trying your idea it worked. Thanks again.

Reply

This looks really good. Is there any way you could use this code to redirect the user to custom 404 pages based on the country they are coming from? Or does anyone know of something like this?

Reply

Does google allow adsense on the 404.php page?

I have had a quick look through google but can’t really determine if they allow this or not.

Great information on setting up the 404 page, thanks

Reply

Jeff  Replied:

This post is about WordPress not Google.

Reply

Your info on 404 worked great for me, many thanks for posting the information. John

Reply

Thank you Jeff for this article. I have been searching for how to do this for about an hour or so in the WP documentation with no avail. As I already have a 404.php file that come with the theme, I pasted your 301 redirection code at the first line and it worked like a charm. thanks again for your help.

Reply

Sometimes 404.php may not working, so add this text to .htaccess file in root directory:

ErrorDocument 404 /index.php?error=404

If it not exist, you may create it.

Regards

Reply

Thanks so much for this tip. I had started ranking for my blog posts, and people were clicking on them and getting 404 errors. I realized that I had followed an recommendation to change my permalinks to include the category name and that was why. So I switched back, but still changed my 404 message to a better one. I was wondering if the code you said to post in the 404.php was supposed to actually cause the page to redirect automatically? If so, I must have placed in in the wrong spot. If not, do you know of a way to do that?

Thanks

Reply

Leave a comment
Drive-By comments will be deleted! Like "Thanks for sharing", "Nice post", or any other text that doesn't contribute to the discussion. If you ask a very specific question about your particular WordPress theme, it will probably be deleted. I can fix your WordPress site but for a price. If you ask a question like "Why don't my blog work right", it will be deleted. If you ask a question about your WordPress site without a leaving a link to it, it will be deleted. The point is to ask questions and or comment on the the actual content of the post, and to please use some common sense. All comments are moderated and will appear when approved. Thank you.

(required)

(required)