We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 48586
    • 78 Posts
    How can I insert something into my current URL path? For example, if a user tries going to www.mysite.com/packages I would like to update the URL to be www.mysite.com/tampa/packages

    I created a snippet with the following code:

    switch ($_COOKIE['market']) {
        
    case "tampa":
      $url = $_SERVER['REQUEST_URI'];
      switch (true) { 
        case strstr($url, 'packages'):
          $currenturl = str_replace('/packages','/tampa/packages/',$url);
          header("Location: ".$currenturl);
          echo $modx->sendRedirect($currenturl, array('type' => 'REDIRECT_HEADER'));
          break;
        }
        break;
    
      // etc for other markets
    }


    I've even tried manually inserting the URL so one of the lines looked like:

    echo $modx->sendRedirect("/tampa/packages/", array('type' => 'REDIRECT_HEADER'));

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

    • discuss.answer
      • 3749
      • 24,544 Posts
      You don't need the header() line and you need to remove the word 'echo.'

      I think a better approach would be a redirect in .htaccess based on the cookie. It should give you significantly faster page loads. It's tricky, though, because it's easy to create an infinite loop. http://stackoverflow.com/questions/3978726/how-to-do-htaccess-redirect-based-on-cookie-value
        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
        • 48586
        • 78 Posts
        Thank you again BobRay, I will definitely look into doing this in htaccess, it sounds like a much cleaner solution.