We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 6659
    • 18 Posts
    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.
      • 4310
      • 2,310 Posts
      Looks really useful.
      Will test and report back smiley
      Thanks Dave.
        • 7231
        • 4,205 Posts
        Cool idea. This can be very handy. wink
          [font=Verdana]Shane Sponagle | [wiki] Snippet Call Anatomy | MODx Developer Blog | [nettuts] Working With a Content Management Framework: MODx

          Something is happening here, but you don't know what it is.
          Do you, Mr. Jones? - [bob dylan]
          • 27708 MODX Staff
          • 2,502 Posts
          Holy Cow! This is awesome on wonderful! Sure its useful but this plugin actually helps push MODx into being yet again THE best SEO CMS out there. No more 401s on changed urls. Automatic 301s are something people have wanted for a very long time.

          Thanks Dave, I’ll try it out very soon.

          Cheers,

          Jay
            Author of zero books. Formerly of many strange things. Pairs well with meats. Conversations are magical experiences. He's dangerous around code but a markup magician. Blog ✦ Twitter ✦ LinkedIn ✦ GitHub
            • 6659
            • 18 Posts
            Thanks chaps!

            I’m concerned about how it will perform on larger sites (might add significant delay when saving documents) - will rewrite there if needed. If you have a larger site and are trying this plugin, please let me know how it performs and how many documents you have.

            Quote from: smashingred at Apr 19, 2009, 07:02 AM
            Automatic 301s are something people have wanted for a very long time.

            I’m just happy to be able to contribute! Is there a list somewhere of most-requested resources or ideas?
              • 7231
              • 4,205 Posts
              I am wondering how this will handle duplicate aliases? MODx will prevent/allow duplicate aliases but if in the process of renaming documents you may have a case where there are multiple documents with the same alias in the history.
                [font=Verdana]Shane Sponagle | [wiki] Snippet Call Anatomy | MODx Developer Blog | [nettuts] Working With a Content Management Framework: MODx

                Something is happening here, but you don't know what it is.
                Do you, Mr. Jones? - [bob dylan]
                • 6659
                • 18 Posts
                Quote from: dev_cw at Apr 19, 2009, 07:40 AM
                I am wondering how this will handle duplicate aliases?

                It automatically returns the most recent match. Here’s how it will handle renaming and deletion:


                • Document A has the alias "widget", and then it is changed to "ex-widget". At this point, "widget" redirects to "ex-widget".
                • Document B is then given the alias "widget". "widget" no longer redirects - just points directly to Document B.
                • Document B is then given a new alias, "new-widget". "widget" now redirects to "new-widget".
                • Document B is then deleted and the trash is emptied. "widget" now redirects to Document A at "ex-widget" again.
                • Document A is deleted but the trash is not emptied. "widget" still redirects to "ex-widget".
                • The trash is emptied. "widget" no longer redirects, and just returns a 404 (or rather, is handled by MODx as usual).

                The only problem situation (that I know of so far) is when there’s a document that was once published and isn’t any more, but isn’t deleted, and uses the same alias as another document used to use. This can be worked around, but I decided to keep it simpler as this should be a very rare occurence.
                  • 7231
                  • 4,205 Posts
                  Thanks for the explanation. I will say it again, very cool idea grin
                    [font=Verdana]Shane Sponagle | [wiki] Snippet Call Anatomy | MODx Developer Blog | [nettuts] Working With a Content Management Framework: MODx

                    Something is happening here, but you don't know what it is.
                    Do you, Mr. Jones? - [bob dylan]
                    • 16183
                    • 1,390 Posts
                    Quote from: DaveChild at Apr 19, 2009, 07:28 AM

                    Thanks chaps!

                    Quote from: smashingred at Apr 19, 2009, 07:02 AM
                    Automatic 301s are something people have wanted for a very long time.

                    I’m just happy to be able to contribute! Is there a list somewhere of most-requested resources or ideas?

                    Probably this? http://modxcms.com/forums/index.php/board,3.0.html

                    Thx for the contribution smiley

                    cheers/k
                      • 33337
                      • 3,975 Posts
                      This is cool plugin, I used VirtualAliases by Brian in the past, but this plugin takes care of the redirects automatically. A bit of AI wink

                      Thanks for the contribution.
                        Zaigham R - MODX Professional | Skype | Email | Twitter

                        Digging the interwebs for #MODX gems and bringing it to you. modx.link