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 think I need to revise my "competent" PHP claim, hehe. I’m understanding the concepts (and I really appreciate all your help, by the way), but I can’t seem to get the syntax correct. Here’s what I’ve got so far. Please don’t laugh, because I know this isn’t even close to right, but can you help me understand what I’m doing wrong? Besides the completely wrong formatting, what I’m not sure about is how to remove the current site’s root address (www.example2.com) and replace it with the new site’s address (www.example1.com). Also, will I need the trailing slash between the root and the subdirectory?


    $current = $_SERVER['REQUEST_URI'];
    $other = "http://www.example1.com/"
    $new = $current - $modx->config['site_start']
    $url = $other + $new
    
    // Get a reference to the event
    $e = & $modx->Event;
    switch ($e->name) {
       case "OnPageNotFound":
           $modx->sendRedirect($url)
        break;
    }
    
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      $modx->config[’site_start’] will be the ID of the document specified to be the home page, usually 1. I don’t think you want that.
        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
        • 28042 ☆ A M B ☆
        • 24,524 Posts
        Put
        echo $_SERVER['REQUEST_URI'];

        in the plugin and see what exactly you get. It should just be the URI, minus the domain part. So the whole URL to feed the sendRedirect function would be
        'www.example1.com' . $_SERVER['REQUEST_URI']
          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
          • 28042 ☆ A M B ☆
          • 24,524 Posts
          Ok, here’s mine that worked; keep in mind that my local MODx installation is in a subdirectory of my web root, so my REQUEST_URI needed to get the first part (1.0-5352) removed. You won’t need that part unless your installation is also in a subdirectory.
          $uri = $_SERVER['REQUEST_URI'];
          $array = split('/', $uri);
          $url = 'http://www.sottwell.com/' . $array[2];
          
          $modx->sendRedirect($url);


            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
            It works!!! I can’t thank you enough. It feels really good to develop something from scratch like this, even if I did need a ton of help. This will solve a lot of headaches for me down the road, and hopefully it will make the switching back and forth of URLs even more transparent to the user.

            My end result is exactly like yours, except without the array:

            $uri = $_SERVER['REQUEST_URI'];
            $url = 'http://www.example1.com/' . $uri;
            
            $modx->sendRedirect($url);


            Thanks again. I’m new to this sort of original creation, but is this something that should go in the repository or the wiki, or do you think it’s too specific to be of much use to anyone else?
              • 20413
              • 2,877 Posts
              This is sure worth a post in the wiki!!

              This plugin is nice --> http://modxcms.com/forums/index.php?topic=31019.0 smiley
              Tons of MODx candy is hidden in the forums.
                @hawproductions | http://mrhaw.com/

                Infograph: MODX Advanced Install in 7 steps:
                http://forums.modx.com/thread/96954/infograph-modx-advanced-install-in-7-steps

                Recap: Portland, OR (PDX) MODX CMS Meetup, Oct 6, 2015. US Bancorp Tower
                http://mrhaw.com/modx_portland_oregon_pdx_modx_cms_meetup_oct_2015_us_bancorp_tower
                • 813 ☆ A M B ☆
                • 326 Posts
                Wow -- it’s amazing how much good stuff is buried on here. I love MODx, but I do wish the "extras" were more organized and easier to track down. Thanks for the tip. I’ll see if I can work this solution into a how-to for the wiki sometime soon.