Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Alan Mattanó
12,329 PointsHow do I create a custom 404 error page correctly? (2015)
How do I create a custom 404 error page correctly?
I make my nice 404 error page. But now I "don't want it to appear in Google search results. In order to prevent 404 pages from being indexed by Google and other search engines", I have to make sure that my webserver returns an actual 404 HTTP status code when a missing page is requested.
How do I set up and get a custom error page to return a 404 header? Do i have to include special notes in the header?
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Page Not Found</title>
<style>
/* css style code */
</style>
</head>
<body>
<h1>404 Page Not Found<br></h1>
<p>Whoops!
<center><b><!--#echo var="REDIRECT_URL" --></b></center>
</p>
<p>Is no longer at this location or the URL may be misspelled.
Hit “back” button on your browser or go to my webpage
<a href="http://<!--#echo var="SERVER_NAME" -->">
<!--#echo var="SERVER_NAME" --></a>
</p>
</body>
</html>
1 Answer
Tim Knight
28,888 PointsAlan,
It all really comes down to your server infrastructure. If you're hosting it somewhere you might want to reach out to your hosting company's support team to ask their preferred process. However if you're hosting it on a Apache-based web server the best way to do this is from the .htaccess file. You just need to add a line to reference your 404 document.
ErrorDocument 404 /404.php
This assumes that you have a file called '404.php' right on the root of the site. Apache will pull this data to respond to not found requests without changing the address in the browser. Because of that it's not something that will get indexed, because you're not linking to it. However if you're concerned about Google indexing your 404 you could block it by adding it to a Robots.txt file on the root of your site. See http://www.robotstxt.org/ for further details on that.