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

    I have a tricky rewrite/redirect question for you. I’m running a MODx site with friendly URLs. The thing is that the old CMS I was using created a URL like: www.example.com/index.php?id=articles&n=cherrypie and I have some nice links pointing to this old URL. My question is, how can I 301 redirect this old url to my new one: www.example.com/articles/cherry-pie.html in order to pass the link value and avoid showing 404’s to the visitors coming to the old URL?

    I have succesfully created 301-redirects in the .htaccess file for other URLs, but when it comes to the index.php, MODx seems to be very "fragile".

    Any help would be appriciated!

      • 15667
      • 11 Posts
      I had a similar problem migrating from Mambo. My solution was to hack the MODx index.php file to detect requests for the old Mambo URLs and return redirects or 404s as appropriate. Very ugly but it works.
        • 9207 ☆ A M B ☆
        • 2,475 Posts
        I would stick to changing .htaccess instead of hacking a core MODx file.

        You should be able to do a standard 301 redirect, just make sure you put it ABOVE the friendly url code. That way the url will go from:

        http://www.example.com/index.php?id=articles&n=cherrypie

        to

        http://www.example.com/articles/cherry-pie.html

        (after your 301 rule changes it) to

        http://www.example.com/index.php?q=articles/cherry-pie.html

        (after MODx’s .htaccess rules change it for friendly urls)

        And that last version is what MODx needs in order to render the page.
          • 5340
          • 1,624 Posts
          hmmm,

          You might be able to get something done with a plugin.
          You can activate the plugin at "OnWebPageInit" event or something like this.

          Maybe "OnPageNotFound" can do the trick. smiley

            • 25803 ☆ A M B ☆
            • 721 Posts
            There’s a plugin: http://modxcms.com/extras.html?view=package/view&package=276
              • 9207 ☆ A M B ☆
              • 2,475 Posts
              I just tried my hand at some 301s....

              in my .htaccess file:
              redirect 301 /old/page.html http://www.domain.com/new/page
              # OR
              RewriteRule ^old_page/page.html $ http://www.domain.com/new/page [R=301,L]
              


              I noticed that the redirect 301 option ended up forwarding correctly, but it tacked on the q query string on the new page, e.g. this is what I’d see after navigating to www.domain.com/old_page:

              http://www.domain.com/new/page?q=old/page.html

              Technically, it worked, but not quite what I wanted. A bit more research landed me on the RewriteRule option, but be careful with your regular expressions! Notice that the RewriteRule option does not use the leading slash (/)! It just uses the page name, but you don’t need to escape any special characters.

              Just put this right above your friendly URL section in your .htaccess file and you should be good. The above works for redirecting urls on one site, not to a new site.
                • 9207 ☆ A M B ☆
                • 2,475 Posts
                I think you might need to include the RewriteCond counterpart to that rule:

                RewriteCond %{HTTP_HOST} ^www\.domain\.com [NC]
                RewriteRule ^old/page.html$ http://www.domain.com/new/page [R=301,L]


                Note that in the first line, you DO need to escape special characters (the dots) with a backslash.

                Sorry I can’t be more certain of this rule... I’m seeing some really strange things on my server and I can’t isolate what’s going on.