This plugin is designed to manage changes to URLs. If you change a document alias, and you want to redirect requests for the old address to the new one, you currently need to add a line to your htaccess file.
(This is especially a problem when building sites for clients, who will change page URLs without understanding the implications of doing so.)
This plugin maintains a list of all aliases given to any document. If the "Page Not Found" event is called, the plugin checks to see if the alias the user has requested used to match a document. If so, it redirects the user to the current document address (with a 301 header for SEO goodness). If not, it lets the page continue loading as normal.
Requirements
I’ve written and tested this with MySQL 5.0.51a. It uses subqueries, so I’m not sure how old a MySQL install it will run on, though I believe 5 and up should be fine. You will need to create a new table in your MODx database to use this plugin.
Usage
First, run the following query against your database (
note - replace "modx_" with your table prefix, if you have one, and check that the character sets and storage engine match the other tables in your database):
CREATE TABLE `modx_site_content_url_history` (
`url_history_id` int(10) unsigned NOT NULL auto_increment,
`content_id` int(10) unsigned NOT NULL,
`alias` varchar(255) NOT NULL,
PRIMARY KEY (`url_history_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='Contains history of document aliases.';
Next, paste the contents of the attached file into a new plugin called "URLHistory".
Finally, ensure the following events are checked for this plugin:
- OnDocFormSave
- OnEmptyTrash
- OnPageNotFound
And that’s it. When you save a document, the URL history is updated (it grabs all aliases that are currently missing from the history - the first time you run this it will grab all aliases). If you empty the trash, history for documents that no longer exist is wiped. And when the user lands on a missing page, the alias they requested is checked against the history to see if they can be redirected.