Persisting data through legacy routes

Usually when a website is migrated to a new version or system, old URLs will break. To circumvent this problem, legacy URLs are redirected to their new destination through some kind of router. CakePHP offers this functionality through Router::redirect() and the RedirectRoute class. But how do you persist URL parameters (like IDs) to the new URL? It's pretty simple.

When I launched the new version of this site, I had to setup legacy routes for blog posts. The old URLs were structured as so blog/read/[id]/[inflected_title], where the new ones are structured like blog/read/[slug]. From the looks of it, no parameters are the same, so this will require a middleman controller. First, I need to map the redirect using the persist option (this is crucial).

Router::redirect('/blog/read/:id/*', array('controller' => 'jump', 'action' => 'blog'), array(
	'id' => '[0-9]+',
	'pass' => array('id'),
	'persist' => array('id')
));

This route will redirect the old URL to JumpController::blog($id) while passing the ID from the URL as the first argument. The JumpController is used to power my tiny URLs — so it now serves 2 purposes. Next up is defining the action logic.

public function blog($id = null) {
	if ($blog = $this->Entry->getById($id)) {
		$this->redirect(array('controller' => 'blog', 'action' => 'read', $blog['Entry']['slug']), 301);
	}
	throw new NotFoundException();
}

When the action is called, the code attempts to find a blog entry that matches the ID. If an entry is found it will redirect with a 301 (Moved Permanently), else it throws a 404 (Not Found). That's all there is too it!

Fixing malformed urls in Mint

Recently I moved to this new domain, I wanted to use my old Mint database and keep all my statistics. So I transferred my Mint licenses, installed Mint 2 on the new domain, updated Mints DB configuration and added the JavaScript tag to start tracking. Everything was up and working, but there was one big problem, all my urls were malformed and escaped. They looked like the following (within any pane but search):

milesjohnson%20%7E%20Contact%20Me - Page Title
http%3A//milesj.me/blog/ http%3A//milesj.me/bio/ - Referrers

It took me a while to figure out the problem, I even sent a bug report to Shaun Inman himself. This is what I found, and how to fix it (Some of these may not apply to you, but it is always good to double check).

Using the right pathing

If you go into your Mint preferences, check your "Mint Location" setting. If your website always has the www. prefix, make sure your location setting has it as well and if you do not use the www. prefix, remove it. If you use both methods (like me), be sure to choose only 1 way of linking items. If you are hosted by Dreamhost, you can manage your domains and change its settings to always default to one of the methods. This is what my preferences look like:

Your URL: http://www.milesj.me/
// Mint Preferences
Site Domain(s): milesj.me
Mint Location: http://www.milesj.me/mint
Disabling mod_rewrite

If you or your server has enabled mod_rewrite, either for the RewriteEngine, redirecting or other means, you will need to create an .htaccess file and place it in the root of the /mint/ directory. Within this .htaccess file, all you need to put is RewriteEngine off.

Fixing Mint's DB config after moving domains

If you have transferred licenses and used the same Mint DB on both domains or switched the Mint folder to a new domain, only this next section applies to you. You can either do it manually (below) or read the instructions here.

Since moving the Mint installation to another domain, your old config becomes outdated. To fix this problem, go to your Mint panel (probably in /mint/) and type this in your address bar: http://yourdomain.com/mint/?moved. Your config should now be updated to the new installation and should be working properly.

I hope this has been helpful to someone. Its always best to write about your experiences in the hope that someone else runs into the same problem! If you are still having problems, you can visit Mint's troubleshooting forums.