Please note that you may have been redirected (you might like to make a note of the URL in the address bar of your browser and update accordingly) This is a permanent archvie but is no longer actively maintained. Please visit http://joshuaink.com for the latest updates.
Textpattern 404 Voodoo
Thursday November 4, 2004
I did a post a while back about the 404 pages on Textpattern and it wasn't right, it didn't work as it should; so I have had another play and the results are much more pleasing.
Important! I did experience some weird problems whilst implementing this; problems which didn't make sense and seemed to cure themselves when I emptied out my browser cache. I advise much caution if you decide to try this!
If you are using style sheet switching, read this first!
What you will need
- Textpattern in Clean URL mode.
- The If_Category plug-in.
- The zem_redirect plug-in (<txp:zem_redirect /> added to the very first line of your page template).
- your .htaccess file.
The zem_redirect plug-in is very powerful and useful as is and you may like to just give it a whirl on it's own to see if it suits your needs. For me though I like to fiddle and I wanted to do something a bit different.
Firstly, I know that I have some dead links on other peoples sites e.g. "joshuaink.com/folio.htm" and this is no real problem I can sort that out using .htaccess. The "problem" I really have is within textpattern itself. If you are running TXP without zem_redirect, try this: Open up your site and go to a post e.g. "/article/30/rebuild-moderate-after-mod" and then modify the URL to something silly like "/article/30000/this-page-dont-exsist". Note that I have kept the "article" part of the URL intact as that is my default "section" for posts and this is the "problem" bit. The result you are probably getting is just your template page with no actual post, essentially you are drawing a blank, this is not so good if you decide to move a post to a new section or delete it, the old URL for that moved post will have this very effect when visited.
My thinking was, wouldn't it be nice to take the user straight to my "archives" section with a wee note telling them why they arrived there, go ahead, try it.
404 category
Referring back to my notes on setting up my archives with the ability to search through content by category gave me the idea of adding a new category called 404. So, having created said "article category" within Content > Organise, I added the following code to my archives template:
- <txp:mdm_if_category category="404">
- <div class="alertbox">
- <p><strong>Oops!</strong> you have been redirected to the archives because the page you are looking for can't be found. If you still can't find what you are looking for from here, please <em>do</em> <txp:email email="john@joshuaink.com" linktext="get in touch" title="Contact Me" /> and I will try and help if I can.</p>
- </div>
- </txp:mdm_if_category>
As this suggests if the the 404 category is selected then show this "alertbox".
.htaccess
For redirection to this "section" and "category" from dead links outside of TXP e.g. "joshuaink.com/folio.htm" I added the following to my .htaccess file
- ErrorDocument 404 http://www.joshuaink.com/archives/?c=404
Job done.
zem_redirect hack
Let me just say again that the zem_redirect plug-in is very cool, you may not need to do this but I did it anyway.
I had a quick look at the options for the plug-in and couldn't see a way to replicate the effect created with .htaccess so I hacked the plug-in, changing this (around line 40):
- if ($notfound) {
- header("HTTP/1.1 404 Not Found");
- if ($errorid) {
- $pretext["id"] = $errorid;
- $zem_redirect_lock = 1;
- textpattern();
- exit;
- }
To this:
- if ($notfound) {
- header("Location: /archives/?c=404");
- if ($errorid) {
- $pretext["id"] = $errorid;
- $zem_redirect_lock = 1;
- textpattern();
- exit;
- }
Let me be honest, I don't really quite know what I have done to the plug-in but it seems to work, so now when I delete or move anything, users go straight to my archives page with a helpful message attached.
Note: At my first attempt I added a message to my home page but thought archives was a better spot, I imagine you could re-direct anywhere you want.



