EE Insider Tips
Sponsored by Mijingo's EE 2 Screencasts
ExpressionEngine mini-howtos created by the EE Insider community.
Handle incorrect URL’s properly - avoid search engine penalties and help users with 404 error pages.
It is very important to make sure you handle invalid URL’s properly within your Expression Engine website.
By default, if you visit a URL that points to an invalid entry for a weblog, instead of showing an error ExpressionEngine will generally try to second-guess what you were trying to find by looking at the URL and displaying the ‘next best/most relevant’ entries (providing your weblog entries tag dynamic preference is set to ‘on’).
For example, if you were to visit http://www.example.com/news/article/doesnt-exist, and your ‘article’ template in the ‘news’ template group contained the following:
{exp:weblog:entries weblog="news" limit="1" dynamic="on"}
<h2>{title}</h2>
{body}
{/exp:weblog:entries}
Yet there is no entry in the news weblog with the url_title ‘doesnt-exist’, EE will still serve you up a normal page with the next nearest news article.
Similarly, if you visit a URL where segment 1 is not a valid template group or segment 2 is not a valid template, EE will instead serve up the index template of your template group as opposed to an error page.
The problems become clear - visitors can easily make a typo in the URL and expect certain content to appear, only to be served a random article or page with no errors making your website seem unreliable or confusing.
Not only this, but you could also potentially get penalised by search engines because if they pickup multiple links (e.g. the correct link to an article plus an incorrect link to an article) and they all return the exact same content, the search engines will tend to mark it as duplicate content with the assumption it was done on purpose to try and boost your rankings for content on that page, which in turn will do the opposite and cause neither links to be ranked highly (or at all) and could also damage your overall ranking.
However luckily as of ExpressionEngine version 1.6.5 there is an easy fix to this problem.
Firstly, head over to your Global Template Preferences (Templates > Global Template Preferences) and enable the option titled ‘Strict URLs’.
Then, also in the Global Template Preferences, make sure you have set a template to use as the 404 error page. You can then edit this template and write a message to inform the visitor that the link they are viewing is incorrect as to avoid any confusion.
Lastly, amend any weblog entries tags where you have set ‘dynamic=“on”’ to also include the ‘require_entry=“yes”’ parameter and then add a {no_results} conditional tag to tell EE what to do when the URL points to an invalid entry, as shown in this amended example:
{exp:weblog:entries weblog="news" limit="1" dynamic="on" require_entry="yes"}
<h2>{title}</h2>
{body}
{if no_results}
{redirect="404"}
{/if}
{/exp:weblog:entries}
So, this time when you visit http://www.example.com/news/article/doesnt-exist, you will be redirected to the 404 template with your custom user-friendly error message, and search engines will recognise that the page does not exist and therefore will not count it as duplicate content.
Email
Print
Post to Twitter
Post to Delicious
JonnyUK — 13:31 on 06.10.2009
Even eeinsider.com could benefit from this tip! You will notice if you visit a URL like ‘http://eeinsider.com/random_link’, instead of redirecting to a 404 page, Expression Engine is serving up the index template of the default template group, which contains the exact same content as http://eeinsider.com/. Search engines will see this is as duplicate content which could harm rankings for that page!
Add Your Comment?
You must have an EE Insider account to post comments on Insider Tips. It's fast, easy and hassle-free.
Sign up now (or login).