We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 26931
    • 2,314 Posts
    Hi everybody,

    i try to redirect a couple of old URLs to the new ones generated by MODx (using friendly URLs). found this MODx plugin "Virtual Aliases" http://www.stanback.net/code/modx/virtual-aliases.html ...but it seems not to work. (for me)

    typing in the old URL will show me the 404 page. btw. the installation is in a subfolder on my localhost.

    has anyone experience with this plugin, or got it working?

    thanks, j

    edit: also tried it with an 1.0.2 installation on a subdomain. + with friendly URLs & TransAlias plugin disabled
      • 26931
      • 2,314 Posts
      here’s the plugin code, maybe someone can spot something that won’t work with the current (1.0.4) MODx version:

      //<?php
      // Virtual Aliases
      // version 0.0.1
      // Allows for an unlimited number of custom aliases per page.
      // By Brian Stanback @ www.stanback.net
       
      // On Install: Check the the "OnPageNotFound" box in the System Events tab.
      // Plugin configuration: &aliasesTV=Aliases TV name;string;Aliases
       
      // For overriding documents, create a new template variabe (TV) named
      // Aliases with the following option:
      //    Input Type: Textarea (Smaller)
      // 
      // Aliases should be added to the TV, one per line. 
      // Omit any leading or trailing slashes as well as the default suffix (usaully .html)
       
      // Begin plugin code
      $e = &$modx->event;
       
      if ($e->name == "OnPageNotFound") 
      {
         // Retrieve requested path + alias
         $documentAlias = $modx->documentIdentifier;
         $documentAlias = ($modx->virtualDir == "") ? $documentAlias : $modx->virtualDir . '/' . $documentAlias;
       
         // Search TVs for potential alias matches
         $sql = "SELECT tvc.contentid as id, tvc.value as value FROM " . $modx->getFullTableName('site_tmplvars') . " tv ";
         $sql .= "INNER JOIN " . $modx->getFullTableName('site_tmplvar_templates') . " tvtpl ON tvtpl.tmplvarid = tv.id ";
         $sql .= "LEFT JOIN " . $modx->getFullTableName('site_tmplvar_contentvalues') . " tvc ON tvc.tmplvarid = tv.id ";
         $sql .= "LEFT JOIN " . $modx->getFullTableName('site_content') . " sc ON sc.id = tvc.contentid ";
         $sql .= "WHERE sc.published = 1 AND tvtpl.templateid = sc.template AND tv.name = '$aliasesTV' AND tvc.value LIKE '%" . $modx->db->escape($documentAlias) . "%'";
         $results = $modx->dbQuery($sql);
       
         // Attempt to find an exact match
         $found = 0;
         while ($found == 0 && $row = $modx->db->getRow($results))
         {
            $pageAliases = explode("\n", $row["value"]);
            while ($found == 0 && $alias = each($pageAliases))
            {
               if (trim($alias[1]) == $documentAlias)  // Check for a match
                  $found = $row["id"];
            }
         }
       
         if ($found)  // Redirect to new document, if an alias was found
         {
            $pageUrl = $modx->makeUrl($found, '', '', "full");
            $modx->sendRedirect($pageUrl, 0, "REDIRECT_HEADER", "301");  // Send a permanent redirect
            exit(0);
         }
       
         header("HTTP/1.0 404 Not Found");
         header("Status: 404 Not Found");
      }

        • 12379
        • 460 Posts
        No, doesn’t work for me either. Could really use it right now. I’ll try contacting the author...
          Mostly harmless.
          • 1932
          • 137 Posts
          I’ve posted an updated version at http://www.stanback.net/code/modx/virtual-aliases.html. It’s working with my configuration but your mileage may vary. If it’s still not working after the update, I can send you some code to try running in order to help debug your environment.
            • 26931
            • 2,314 Posts
            Hey ApoXX,

            thanks a lot! i will try and report back. j
              • 12379
              • 460 Posts
              No luck here...
                Mostly harmless.
                • 1932
                • 137 Posts
                Please be more specific other than just telling me it’s not working:

                Have you checked the OnPageNotFound?

                What did you name the aliases TV?

                What do the aliases you’ve defined look like?

                Are you receiving a MODx-based 404 error when visiting aliased URLs?

                What is the value of $documentAlias being set to?
                  • 12379
                  • 460 Posts
                  Have you checked the OnPageNotFound?
                  Yes, under "Parser Service Events" as it didn’t exist under "System"

                  What did you name the aliases TV?
                  Aliases

                  What do the aliases you’ve defined look like?

                  http://dev.site.com.au/test/whatevs
                  http://dev.site.com.au/test/slider

                  Are you receiving a MODx-based 404 error when visiting aliased URLs?
                  Yes

                  What is the value of $documentAlias being set to?
                  As per original code:
                  $documentAlias = str_replace($modx->config['friendly_url_suffix'], '', $_REQUEST['q']);

                    Mostly harmless.
                    • 1932
                    • 137 Posts
                    Thanks - looks to be correct so far...

                    Can you list your configuration settings for the Friendly URLs tab under configuration?

                    One more thing, can you try adding a quick, temporary, echo statement to inspect the value of $documentAlias.

                    $documentAlias = str_replace($modx->config['friendly_url_suffix'], '', $_REQUEST['q']);
                    echo $documentAlias;
                    exit;
                    
                      • 12379
                      • 460 Posts
                      One more thing, can you try adding a quick, temporary, echo statement to inspect the value of $documentAlias.
                      test/whatevs

                      Can you list your configuration settings for the Friendly URLs tab under configuration?
                      Use FriendlyURLs: Yes
                      Friendly URL Prefix:
                      Friendly URL Suffix:
                      Use Friendly URL aliases: Yes
                      Use Friendly URL alias path: Yes
                      Allow duplicate URL aliases: Yes
                      Automatically generate URL alias: Yes
                        Mostly harmless.