We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 28384
    • 2 Posts
    This is my first attempt at a modx plugin, I realise I have done a few things in the wrong way and would apreciate people with more experience with modx improving my code.
    I am posting this to try and help others with a similar issue as I couldnt find a contribution that did what I needed.


    Problems:
    I have a site designed with Modx that has been online without SEO urls.
    I have recently enabled SEO url’s and gogole is now indexing all pages twice.

    Addiitonally: Pages I have deleted show a 404 modx error page to google and arent being removed from google index.



    Solution:
    New modx plug in to force pages to use friendly url’s and also to redirect to home page if requested page have been deleted, or in our case stil there but not published.

    I have put this code here to help others with same problem.
    I would really apreciate if someone with more modx knowledge would fix my code so we can make this available as a download.


    INSTALLATION:
    CREATE A NEW PLUGIN: (Name: redirect_into_friendly_url)

    Paste this code as the plug in code:
    // Get name of requested page
    $startpage = $_SERVER["REQUEST_URI"];
    
    // take a sub string out of it 
    $startpageindex = substr($startpage,1,10);
    
    //See if the substring has index.php in it
    if ($startpageindex == 'index.php?') {
      // This page needs to be redirected
    
      // Get the page id from the parsed string
          $startpageid = ereg_replace("[^0-9]", "",$startpage);
    
      // Get the table name from modx vars
          $contentvalues = $modx->getFullTableName("site_content");
    
      // Build and execute sql statement to get the alias for this page id
          $sql = "select alias from " . $contentvalues . " where id = " . $startpageid;
    
    
          $result = $modx->db->query( $sql );
          if( $modx->db->getRecordCount( $result ) >= 1 ) {
    
             $row = $modx->db->getRow( $result );
             $id = $row['alias'];
             $url = '/' . $id . '.html';
    
             $modx->sendRedirect($url, 0, 'REDIRECT_META', '301');
             return;
    
          } else {
            // This page not found - redirect to home
             $modx->sendRedirect('/', 0, 'REDIRECT_META', '301');
            return;
          }
    
    } else {
      // This page is fine as it is
    
      return;
    }
    
    
    
    


    EVENTS
    I have set the folllowing check box’s under system events:
    On WebPagePreRender = On
    on PageNotFound = On



    FOLLOW UP
    I realise my code isnt 100% thats is why I am asking for help to make this a useful contribution.
    However, feel free to use my code as a starting point.


    Examples of this working on a live site:
    Non seo url: http://starre.co.uk/index.php?id=47
    Should redirect to: http://starre.co.uk/design.html





    Known issues:
    a) Code will need to be modified if modx isnt in route of hosting package.
    b) I havent put any code in to make sure this is fired on corect event.


    Note: Feel free to mention every additon issues you see, even if you dont have a solution for them.
      • 20413
      • 2,877 Posts
        @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
        • 28384
        • 2 Posts
        I tried that contribution but it didnt do everything I needed.

        Notes:
        The contribution dosent seem to redirect standard web users to the correct page.
        This may correct googles index but it encourages users to use and link to the outdated page url’s until google results are updated.

        I also want those pages I removed from modx that are currently indexed by google as a copy of the 404 page removed from googles index too.
          • 20413
          • 2,877 Posts
          If your plugin does the job use it! smiley
          Check this out http://www.stanback.net/code/modx/seo-strict-urls.html
            @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