x
 
Create New Page
 View Previous Changes    ( Last updated by Bruce2005 )

SEO Friendly 404 pages

It took me a long time, (bruce2005), and perhaps a few additions to EE in recent releases, to be able to finally have a custom 404 served as 404 and not 200 (file found), in all circumstances.

The main reason is to have the search engines remove pages that have been deleted, improperly linked to by other sites, or any other reason. Also there is then no duplicate content issues.

It takes adding PHP code to the 404 page to serve it as 404, (default serves the page as 200-file found), and using if no_results, if segment_2, and if segment_3 are not empty to get this, plus a redirect.

this thread with Derek’s assistance led to me being able to resolve this perfectly.

For now, until I learn a better way, the below with the addition of using .htaccess serves a proper 404 in all cases.

First I set a 404 in templates/global template preferences, site/404
Then I added ErrorDocument 404 /index.php/site/404/ to my htaccess file.

Then in weblogs templates I set:
Note: this works on my site, you may need to experiment (have backup tmpl!)
I never have index.php/article/
always index.php/comments/article/

Index tmpl:
{if segment_2 != ''}
{exp
:weblog:entries weblog="blog" require_entry="yes"  orderby="date" sort="desc" limit="1" disable="trackbacks"}
{if no_results}
{redirect
="site/404"}
{
/if}
{
/if}

{if segment_2
== ''}
{exp
:weblog:entries weblog="blog"  orderby="date" sort="desc" limit="1" disable="trackbacks"}
{
/if}
<h1>{title}</h1>
{body}etc...

Comments tmpl
{exp
:weblog:entries weblog="blog"  orderby="date" require_entry="yes" sort="desc" limit="1" disable="trackbacks"}
{if no_results}
{redirect
="site/404"}
{
/if}
<h1>{title}</h1>
{body}etc...
Note: this works in comments if you always have an article appended, otherwise use index code with segment_3.

Then from this thread use Derek’s code for setting 404 response code in the header, and we is all set.
404 template:

{assign_variable:my_weblog="site"}
{assign_variable
:my_template_group="site"}

<?php
global $TMPL, $OUT;
$OUT->out_type = '404';
$TMPL->template_type = '404';
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
>
etc...

This, with htaccess code, covers domainname.com/nofile/, index.php/nofile/ and index.php/template group/nofile/

You can Check server response codes here.

Category:Howto

Categories: