We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 36926
    • 701 Posts
    Hi,

    Having some issue with a rewrite rule. Trying to do the following redirect:

    If the following url is used:
    /news/latestnews/some-article-2012.html
    the user is redirected to
    /news/2012/some-article-2012.html
    if they enter
    /news/latestnews/some-article-2013.html
    the get redirect to
    /news/2013/some-article-2013.html

    So depending what the year is in the file name they are redirected to the corresponding year directory. Note that the year isn't always the last 4 characters before the .html.

    I have it redirecting to the correct year directory, but i can't append the correct file name.
    What i've got is (this just catches the 2012 ones):
    RewriteCond %{REQUEST_URI} ^/news/latestnews/(.*)$
    RewriteCond %$1 ^(.*2012.*)$
    RewriteRule ^(.*)$ /news/2012/ [R=301,L]
    


    But his just redirects to
    /news/2012
    not
    /news/2012/some-article-2012.html

    I've tried adding $1 to the end of the rewriterule like this:
    RewriteRule ^(.*)$ /news/2012/$1 [R=301,L]
    

    But this appending the whole of the original url and not just the file name. so i end up getting
    /news/2012/news/latestnews/some-article-2012.html

    I'm close, but just need some fine tuning i think on the regex part.

    Any suggestions?

    This question has been answered by BobRay. See the first response.

    • discuss.answer
      • 3749
      • 24,544 Posts
      I think you want something like this. It should handle any year after 2010

      RewriteCond %{REQUEST_URI} ^/news/latestnews/(.*)$
      RewriteRule ^news/latestnews/(.*)(201[0-9])(.*)$ /news/$2/$1$2$3 [R=301,L]
      [ed. note: BobRay last edited this post 13 years, 3 months ago.]
        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
        • 36926
        • 701 Posts
        Quote from: BobRay at Jul 11, 2013, 03:16 PM
        I think you want something like this. It should handle any year after 2010

        RewriteCond %{REQUEST_URI} ^/news/latestnews/(.*)$
        RewriteRule /news/latestnews/(.*)(201[0-9])(.*) /news/$2/$1$2$3 [R=301,L]

        Great thanks Bob, even better solution than as I was expecting to do one for each year. Needed to tweak the RewriteRule ever so slightly as it wasn't working. I've added ^ to the start and removed the first /. Also added $ to the end of the pattern. So it looks like.
        RewriteCond %{REQUEST_URI} ^/news/latestnews/(.*)$
        RewriteRule ^news/latestnews/(.*)(201[0-9])(.*)$ /news/$2/$1$2$3 [R=301,L]
        


        Not sure why the first / was causing problems.

        But thanks again Bob, a great solution. Do you want to edit your answer to include the tweaks so I can flag it as the correct answer as doesn't feel right flagging my own after you've done all the hard work wink
          • 3749
          • 24,544 Posts
          I'm not sure why those changes are necessary either. I tested it with preg_match in PHP, but I think .htaccess rewrite rules are not quite the same. I've modified the post above.
            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