We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 1209
    • 49 Posts
    I am busy with migrating the site from old phpnuke with urls like [tt]/module.php?name=Pages[/tt] to MODx.

    In order to keep search engines happy and informed about new addresses of the pages I need to have a way to send 301 redirect when someone comes to that old page like [tt]/module.php?name=Pages[/tt].

    I have written a plugin that starts OnPageNotFound

    $TVname = 'oldURI';
    
    $current = $_SERVER["REQUEST_URI"];
    
    $table = $modx->getFullTableName("site_tmplvars");
    $TVid = mysql_fetch_object($modx->db->select('id', $table, "name = '$TVname'"))->id;
    $table = $modx->getFullTableName("site_tmplvar_contentvalues");
    $doc = mysql_fetch_object($modx->db->select('contentid', $table, "value='$current' and tmplvarid='$TVid'"))->contentid;
    
    $url=$modx->makeUrl($doc);
    $modx->sendRedirect($url, 1);
    



    This code doesn’t work. Nothing happens. The main (404) page gets open.
    Why?

    How can I put something in some log for debugging?
      • 18397
      • 3,250 Posts
      Please do not double post.

      Give this a try:

      $TVname = 'oldURI';
      
      $current = $_SERVER["REQUEST_URI"];
      
      $table = $modx->getFullTableName("site_tmplvars");
      $TVquery = $modx->db->select('id', $table, "name = '$TVname'","","1");
      $TVrow = $modx->getRow($TVquery);
      $TVid = $TVrow["id"];
      $table = $modx->getFullTableName("site_tmplvar_contentvalues");
      $docQuery = $modx->db->select('contentid', $table, "value='$current' and tmplvarid='$TVid'");
      $docID = $modx->getRow($docQuery);
      $url = $modx->makeUrl($docID);
      $modx->sendRedirect($url, 0, 'REDIRECT_HEADER', '301');
      
        • 1209
        • 49 Posts
        Hmmm...

        Still it doesn’t work.

        How can I log something somewhere for debugging?
          • 1209
          • 49 Posts
          After some debug and optimisation I came to the following code

          $TVname = 'oldURI';
          
          $current = $_SERVER["REQUEST_URI"];
          
          $tmplvars = $modx->getFullTableName("site_tmplvars");
          $contentvalues = $modx->getFullTableName("site_tmplvar_contentvalues");
          
          $docQuery = $modx->db->query("SELECT contentid FROM $tmplvars INNER JOIN $contentvalues ON $tmplvars.id = $contentvalues.tmplvarid WHERE $tmplvars.name = '$TVname' AND $contentvalues.value = '$current'");
          
          $docRow = $modx->db->getRow($docQuery);
          
          $url = $modx->makeUrl($docRow['contentid']);
          $modx->sendRedirect($url, 1, 'REDIRECT_HEADER', '301');
          


          Being debugged in a snippet it shows the correct relative url (/virtualdir/alias).
          But DOES NOT redirect.

          PS.
          [tt]REDIRECT_REFRESH[/tt] and [tt]REDIRECT_META[/tt] do work. [tt]REDIRECT_HEADER[/tt] doesn’t.
            • 33372
            • 1,611 Posts
            The problem may be that header redirects don’t work if any output at all has been sent. So you’d probably need to make sure this plugin fires at the OnWebPageInit event. Even with a blank template I don’t think you can make this work as a snippet.
              "Things are not what they appear to be; nor are they otherwise." - Buddha

              "Well, gee, Buddha - that wasn't very helpful..." - ZAP

              Useful MODx links: documentation | wiki | forum guidelines | bugs & requests | info you should include with your post | commercial support options
              • 22303 MODX Staff
              • 10,725 Posts
              Why not just create a page with the alias modules.php, then have a snippet on that page simply redirect the user to the new page based on whatever your rules are.

              Also, try these for REDIRECT_HEADER...
              $modx->sendRedirect($url, 1, '', 'HTTP/1.0 301 Moved Permanently');

              or
              $modx->sendRedirect($url, 1, '', 'HTTP/1.1 301 Moved Permanently');
                • 1209
                • 49 Posts
                Ok.

                It’s ready.

                $TVname = 'oldURI';
                
                $e = &$modx->Event;
                switch ($e->name) {
                case "OnWebPagePrerender":
                  $to_redirect = $modx->config['to_redirect'];
                  if ($to_redirect > 0) {
                    $modx->config['to_redirect'] = null;
                    $url = $modx->makeUrl($to_redirect);
                    $modx->sendRedirect($url, 0, 'REDIRECT_HEADER', '301');
                  }
                  break;
                case "OnPageNotFound":
                  $current = $_SERVER["REQUEST_URI"];
                  $tmplvars = $modx->getFullTableName("site_tmplvars");
                  $contentvalues = $modx->getFullTableName("site_tmplvar_contentvalues");
                  $docQuery = $modx->db->query("SELECT contentid FROM $tmplvars INNER JOIN $contentvalues ON $tmplvars.id = $contentvalues.tmplvarid WHERE $tmplvars.name = '$TVname' AND $contentvalues.value = '$current'");
                  $docRow = $modx->db->getRow($docQuery);
                  $modx->config['to_redirect'] = $docRow['contentid'];
                  break;
                default: 
                  return; // stop here - this is very important. 
                  break;
                }
                


                I am not sure if [tt]$modx->config[/tt] is the right place to store custom parameters. Seems that $_SESSION doesn’t exist for non-logged-in user.
                  • 18397
                  • 3,250 Posts
                  Quote from: Mark at Mar 17, 2007, 05:57 PM

                  You could write a plugin that OnPageNotFound takes...
                    • 1932
                    • 137 Posts
                    Cool plugin, I’ve been looking to so something very similar. Opengeek’s solution would work but will end up cluttering the document tree, especially when you are dealing with many page redirects.

                    Prerender may be a little late to send the redirect, although it should work fine. Instead of chaining the events and storing data as a configuration parameter, I would use the OnWebPageInit event:

                    $TVname = 'oldURI';
                    
                    if ($modx->Event == "OnWebPageInit")
                    {
                       if (!$modx->documentIdentifier)  // Check for 404
                       {
                          $current = $_SERVER["REQUEST_URI"];
                          $tmplvars = $modx->getFullTableName("site_tmplvars");
                          $contentvalues = $modx->getFullTableName("site_tmplvar_contentvalues");
                          $docQuery = $modx->db->query("SELECT contentid FROM $tmplvars INNER JOIN $contentvalues ON $tmplvars.id = $contentvalues.tmplvarid WHERE $tmplvars.name = '$TVname' AND $contentvalues.value = '$current'");
                          if ($modx->getRecordCount($docQuery) > 0)
                          {
                             $id = $modx->getValue($docQuery);
                             $url = $modx->makeUrl($id);
                             $modx->sendRedirect($url, 0, 'REDIRECT_HEADER', '301');
                          }
                       }
                    }
                    
                    return;
                    

                    This is completely untested but should work. Do you mind sharing your TV config?

                    Edit: Added check for valid result..
                      • 1209
                      • 49 Posts
                      Quote from: ApoXX at Mar 18, 2007, 03:15 PM

                      Cool plugin, I’ve been looking to so something very similar. Opengeek’s solution would work but will end up cluttering the document tree, especially when you are dealing with many page redirects.

                      Prerender may be a little late to send the redirect, although it should work fine. Instead of chaining the events and storing data as a configuration parameter, I would use the OnWebPageInit event:

                      OnWebPageInit would be perfect, but it doesn’t work for some reason. 0.9.6RC1. OnWebPagePrerender seems to be the earliest event (and maybe the only) with which everything starts working.

                      Your code would work with OnWebPageInit, but with OnWebPagePrerender we already are going to show the "404" page (id=1), therefore I can’t use your way of checking the 404ness, because documentIdentifier is already set to 1.

                      ===

                      The TV settings are just a plain text. I put there those old URIs [tt]/module.php?name=Pages&andstufflikethat[/tt]

                      This discussion is closed to further replies. Keep calm and carry on.