The eagle-eyed amongst you (along with those reading via RSS) will have noticed that I’ve changed my permalinks. I decided to go with the domain.com/yy/mm/dd/title.html format. That was the easy part. The hard part was writing an .htaccess file to redirect the old pages (now deleted) to the new ones.
.htaccess files are wonderful things. Every time a browser comes to a site it checks for an .htaccess file and obeys all the rules therein, kinda like having a sign by your front door saying “take your shoes off, put the toilet seat down, smoking in the kitchen only, toilet upstairs on the right, Jimmy and Sammy have swapped bedrooms”. So, when people came here from links elsewhere or via google I didn’t want them to end up with a “404 Not Found page”. I wanted the .htaccess file to say “you’re looking for this file? Well, it’s over here now.” Simple.
I knew how to get Movable Type to automagically generate an .htaccess file that would do this for all 1400 pages and I thought I knew what commands to put in there. What I’ve learned is there are many ways to do this depending on a myriad of things none of which I could find intelligible documentation for. After two hours of searching through various FAQs, including the Apache Server site (of which .htaccess is a part), trying a number of solutions which didn’t work and digging through the Support pages of my hosting service I was about to give up and write the “I’m probably being really thick but…” email to the help desk, when I noticed a user-contributed comment on one of the pages. Bingo. It worked.
Was it worth it? In the short run probably not, but I’ve learned something today, which is never a bad thing. Unfortunately I’ve also reaffirmed my belief that all the information out there on the internet is either aimed at the utterly clueless or the totally clued up. There’s bugger all for the dilettante in the middle.
Excellent, this has been on my blog To Do list for some time and I think you’ve just saved me a lot of research. Now I just have to go back and add titles to my first five months’ worth of posts.
The solution that worked for me was
RewriteEngine on
<MTEntries lastn=”10000″>
RewriteRule <$MTEntryID pad=”1″$>.html http://peteashton.com/<$MTEntryDate format=”%y/%m/%d”$>/<$MTEntryTitle dirify=”1″ trim_to=”20″$>.html
</MTEntries>
The MT code is not the problem – you may have issues with “RewriteEngine on” and “RewriteRule” depending on how Apache is set up on your server (assuming it uses Apache…)
Create a new index template, whack that code in there, give it the filename “.htaccess”, save and rebuild.
Next go to “Weblog Config”, “Archiving” and stick <$MTEntryDate format=”%y/%m/%d”$>/<$MTEntryTitle dirify=”1″ trim_to=”20″$>.html in the “Archive File Template” box for Individual.
Save that and rebuild everything. Then ftp in and delete all the old individual archive pages.
The htaccess file should now redirect old permalink URLs to new. If it doesn’t (either you get a 500 error or a 404) then the adventure begins…
Oh, one othing thing that occured to me, but which I’d never seen written elsewhere: When you move to a directory structure you might find images get broken if, like me, you’ve used relative URLs in the past.
In other words, images/cat.jpg will work in your root directory (where the home page index.html file is) but won’t work when placed in 03/12/24/filename.html (because there is no 03/12/24/images/cat.jpg on your server).
Easy trick, which I noticed when looking through the source code of a Google cache, is to put <base href=”http://peteashton.com/”> within the <head></head> tags which makes all relative URLs come from there.