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
    Okay, PHP and MODx pros -- I have a fun one for you to figure out wink

    I have a site that uses a very inflexible, proprietary content management system. In fact, it’s so inflexible that we were unable to achieve the homepage design we wanted without using Flash, which isn’t a viable option for several reason. So, to get around this problem, I built a second homepage within MODx, and on the homepage of the first site (the bad CMS), I’ve set up a redirect to the second homepage (the MODx one). In other words, typing in www.example1.com will automatically take you to www.example2.com. It’s not a perfect solution, but it was the best option for our situation.

    Now, here’s where I need help. This setup actually works really well, except when someone is on www.example2.com and decides to type the address for a subdirectory that would actually be on the first site. So, if they’re trying to go to www.example1.com/subdirectory but don’t notice the first part of the address as they type, they end up with www.example2.com/subdirectory. This page doesn’t exist, and they hit a 404.

    What I’m wondering is, would there be a way to take the address they input, swap out www.example2.com for www.example.com, and redirect them to that correct page? Because we’re only running a few pages on the MODx site, I’m comfortable with using my 404 page for this, if necessary. My idea is a snippet that would sit on the 404 page, and when it’s hit, do the address-changing and redirecting, similar to how FirstChildRedirect works.

    I know this is a little out of the norm, but it would be a huge help if someone could come up with a solution for this. I am not a PHP expert, so I don’t think I can code the snippet myself. I’d also be fine with a Javascript-based solution if someone has an idea in that direction.

    Thanks!
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      Sounds like a job for htaccess. Does your first site have .htaccess capability?

      There would be no way for MODx to intercept the user’s browser request for another domain. MODx can only react to requests that are referred to the domain it is installed in.
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 813 ☆ A M B ☆
        • 326 Posts
        The first site (www.example1.com) does not. The second one (the MODx-based one) does.
          • 28042 ☆ A M B ☆
          • 24,524 Posts
          Does the first domain’s CMS have the capability of creating custom 404 pages?
            Studying MODX in the desert - http://sottwell.com
            Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
            Join the Slack Community - http://modx.org
            • 813 ☆ A M B ☆
            • 326 Posts
            No, we can’t customize the 404 pages. It’s a very closed box.

            If the 404 page is on the MODx site, why couldn’t MODx react to that? Excuse me if I get my terminology wrong, but the referring page -- the incorrect address that leads the visitor to the 404 page -- WOULD be on the MODx site, and I would think there’s a way for the 404 page to interpret this referral address, change the address to the first site, and send the user there. Does that make sense? All the grunt work will be handled on MODx. The other site is just where the user would be sent to.
              • 28042 ☆ A M B ☆
              • 24,524 Posts
              Ah... excuse me. I just realized I’m understanding your question backwards. Yes, you use a plugin in MODx to direct to where the user needs to go. Use the OnPageNotFound event, and then code the plugin to parse the requested URL and redirect as appropriate.
                Studying MODX in the desert - http://sottwell.com
                Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                Join the Slack Community - http://modx.org
                • 813 ☆ A M B ☆
                • 326 Posts
                NOW we’re on the same page! smiley I realized when I re-read my explanation that I may have confused some people.

                Would you (or anyone else reading this) be interested in writing this plugin, or at least pointing me in the right direction? I am competent in PHP, but FAR from fluent. I definitely can’t write this from scratch by myself.
                  • 28042 ☆ A M B ☆
                  • 24,524 Posts
                  To get you started http://www.sottwell.com/how-plugins-work.html

                  I’ll dig around and see how to get ahold of the request string so it can be parsed.
                    Studying MODX in the desert - http://sottwell.com
                    Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                    Join the Slack Community - http://modx.org
                    • 813 ☆ A M B ☆
                    • 326 Posts
                    Thanks. I’ll see if I can at least get a start on this. Just to be clear, there won’t be any other variables or extenuating circumstances -- it will always be going from example2.com/subdirectory to example1.com/subdirectory. The subdirectory will always be the same, and the root address will just change from the one you’re on (example2.com) to the other (example1.com).
                      • 28042 ☆ A M B ☆
                      • 24,524 Posts
                      Ok, the variable you want to look at in the plugin is the $_SERVER[’REQUEST_URI’];

                      so you should just be able to grab that, and use the $modx->sendRedirect($url) API function with it.
                        Studying MODX in the desert - http://sottwell.com
                        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                        Join the Slack Community - http://modx.org