We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 813 ☆ A M B ☆
    • 326 Posts
    I apologize in advance if this post rambles a bit. I'm sure there's a name for what I'm trying to do here, but I don't know what it is.

    Basically, I'm building a catalog site that will publish a new version once a year. I'd like to structure my urls something like this, and use multiple contexts at the year level, like so:

    http://www.example.com/2011/etc/etc/etc.html
    http://www.example.com/2012/etc/etc/etc.html
    http://www.example.com/2013/etc/etc/etc.html

    However, there is an important need for people who are linking to a page to not have to update their links every year. In other words, the link is stationary, but where the link points changes every year. My first instinct is to still generate links like the above examples, but to also allow people to link to the current year's page like this...

    http://www.example.com/currentyear/etc/etc/etc.html

    ...and then write a plugin that checks the URL for a value of "currentyear" and routes them to the appropriate context (current year could be stored as a system setting). Has anyone done this, or is there an existing plugin? Or am I approaching this from the wrong direction entirely? Any feedback would be appreciated.
      • 813 ☆ A M B ☆
      • 326 Posts
      On Twitter, @splittingred pointed out Redirector:

      http://rtfm.modx.com/display/ADDON/Redirector

      Some clever regex in the "pattern" field could work. Any other suggestions? Or maybe some regex junkies who want to help me with the pattern? ;-)
        • 3749
        • 24,544 Posts
        It's not as elegant, but I would be inclined to just use stristr() and look for 'example.com/2010', 'example.com/2011', etc. in the URL in a plugin connected to OnHandleRequest (be sure to use single quotes) and use $modx->sendRedirect() to send them to the appropriate page.

        You'd have to add a new line every year, but it would be pretty bulletproof.
          Did I help you? Buy me a beer
          Get my Book: MODX:The Official Guide
          MODX info for everyone: http://bobsguides.com/modx.html
          My MODX Extras
          Bob's Guides is now hosted at A2 MODX Hosting
          • 22303 MODX Staff
          • 10,725 Posts
          See this post explaining how to have subfolders routed to specific Contexts. You should then be able to configure your "currentyear" Resource in the main Context to redirect you to the Context representing the current year.

          Also, I would use OnPageNotFound to avoid overriding core routing behavior too early in the request.
            • 813 ☆ A M B ☆
            • 326 Posts
            Thanks, guys! I'm seeing three different options here that all seem pretty good. Opengeek's link seems the most thorough and is exactly what I'm trying to do, but coming up with a regex pattern for Redirector seems pretty simple. Are there any advantages/disadvantages to one or the other? My only concern with Redirector is that I'm sort of leveraging it for an unintended purpose.