We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 37475
    • 25 Posts
    I have a problem. I use friendly urls in MODX Revolution 2.1.5-pl. But I want product pages to be dynamic. All my products are supposed to be listed in site.com/product/ when you click on a product you will go to /product/product-name and load the product product-name in a snippet or something like that.
    Now modx thinks the page is not found and sends me to the start page. What I want is to show /product and maybe send product-name by GET variable.

    How do I solve this?
      • 4041
      • 788 Posts
      I am not the original author of the following, I copied it from the forums many moons ago. It looks like it will fit your purpose, although a few mods for revolution coding may be needed. Hope it helps smiley


      You can author a plugin that uses $modx->sendForward() and is registered to the OnPageNotFound event that does whatever you want. Here is an example:

      Code:
      /*
           OnPageNotFound
      */
      
      /*
      this should be the Resource id of the page containing your view Snippet
      */
      $productResourceId = 21;
      
      $requestURI = trim($_SERVER['REQUEST_URI'], '/');
      $requestParts = explode('/', $requestURI, 2);
      if (count($requestParts) == 2) {
          if ($requestParts[0] === 'store') {
              /*
              this assumes you have a product table with an alias field for specifying the relative product URL as t-shirts/v-neck.html
              */
              $productId = $modx->db->getValue("SELECT `product_id` FROM `product` WHERE `alias` = '{$requestParts[1]}'");
              if (intval($productId)) {
                  /*
                  set the productId into the $_GET variable for your product view Snippet to use
                  */
                  $_GET['productId'] = $productId;
                  /*
                  now forward the request to your Resource containing the product view Snippet
                  (forwarding does not change the requested URL like a redirect)
                  */
                  $modx->sendForward($productResourceId);
              }
          }
      }
        xforum
        http://frsbuilders.net (under construction) forum for evolution
        • 37475
        • 25 Posts
        TY breezer

        Solved it with:

        <?php
        $pages = array('movie' => 5, 'music' => 4);
        
        $requestURI 	= trim($_SERVER['REQUEST_URI'], '/');
        $requestParts 	= explode('/', $requestURI);
        
        if (count($requestParts) == 2) {
            if (array_key_exists($requestParts[0], $pages)) {
        
                $productId = GET ID FROM DATABASE;
              
                if (intval($productId)) {
                   
                    $_GET['productId'] = $requestParts[1];
                    $modx->sendForward($pages[$requestParts[0]]);
        	  
                }
            }
        }