We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 34077 ☆ A M B ☆
    • 64 Posts
    Hey all. I needed to setup a bunch of redirects for a site I was just deploying and didn’t find a plugin that was working fully correctly. Using NG!Redirect as a base I modified it for it to work for me on 1.0.2. Hopefully others will find this useful. In comments at top I did reference the old plugin, hopefully that is the correct way of crediting the original author.

    To install Create a new plugin:
    Name: Redirects
    Description: <strong>0.0.3</strong> Redirects old URL’s to the new ones
    Paste in code below
    Edit $sites array with your old pages and docid to redirect to
    On system events tab select OnPageNotFound

    /*####
    #
    #	Name: Redirect
    #	Author: Three Eyed Bird Design
    #			http://www.threeeyedbird.com
    #	Version: 0.0.3
    #	Date: 1/8/10
    #	Based on original plugin NG!Redirect version 0.0.2 by
    #	Stefan Bruggmann - http://www.ng-projects.ch
    #
    ####
    #
    #
    # Usage:
    # Enter your old Site-Names and the new static ones or dynamic id's to the following array like..
    #
    # Example:
    # $sites = array(
    # 	'The-Old-URL' => 6,
    # 	'Another-Old-URL' => 'A-New-Hard-Link'
    # );
    #
    ####
    */
    
    $sites = array(
    	'/oldpage.html' => 1,
    	'/contactustoday.html' => 6,
    );
    $e = &$modx->event;
    if($e->name == "OnPageNotFound")
    {
    	//Get Current URL
    	$current = $_SERVER["REQUEST_URI"];
    	foreach( $sites as $k => $v ){
    			if( $k==$current ){
    				if( is_numeric($v) ){
    					$obj = $modx->getDocumentObject('id', $v);
    					$url = $obj['alias'].$modx->config['friendly_url_suffix'];
    				}else{
    					$url = $v;
    				}
    				$this->sendRedirect($url, 0, 'REDIRECT_HEADER', 'HTTP/1.1 301 Moved Permanently');
    				exit;
    			}
    	}	
    }

      Jason Bird
      Twitter: @jasonabird @threeeyedbird
      • 11316
      • 81 Posts
      This is awesome you updated this plugin.

      I still can’t get this to work with the id’s.
      But it does work if I just plugin the urls.

      '/oldpage/' => 8, // Doesn't work
      '/oldpage/' => '/newpage/', // Does work


      Thank you! Your a life savor - moving site with over 400+ url changes (seo is built strong with existing site but the technology was old).
        • 34077 ☆ A M B ☆
        • 64 Posts
        I haven’t tested or tried it for a subfolder/category URL, just the an actual html page. Would probably have to be modified to support something/ versus something.html.
          Jason Bird
          Twitter: @jasonabird @threeeyedbird
          • 34077 ☆ A M B ☆
          • 64 Posts
          I’ve updated it to handle different base paths and category/folder URLs now. Here is version 0.0.4
          /*####
          #
          #	Name: Redirect
          #	Author: Three Eyed Bird Design
          #	Version: 0.0.4
          #	Date: 8/20/10
          #	Based on original plugin NG!Redirect version 0.0.2 by
          #	Stefan Bruggmann - http://www.ng-projects.ch
          #
          ####
          #
          #	Version Notes
          #	0.0.4
          #	Added the $base and $siteurl variables for handling different site bases
          #	Added $siteurl for redirect to handle redircting category
          #
          ###
          #
          # Usage:
          # Enter your old Site-Names and the new static ones or dynamic id's to the following array like..
          #
          # Example:
          # $sites = array(
          # 	$base.'The-Old-URL' => 6,
          # 	$base.'Another-Old-URL' => 'A-New-Hard-Link',
          #	$base.'folder/' => 6
          # );
          #
          ####
          */
          
          //Set base url
          $base = MODX_BASE_URL;
          $siteurl = MODX_SITE_URL;
          
          $sites = array(
          	$base.'oldpage.html' => 1,
          	$base.'contactustoday.html' => 6,
          	$base.'folder/' => 7,
          );
          $e = &$modx->event;
          if($e->name == "OnPageNotFound")
          {
          	//Get Current URL
          	$current = $_SERVER["REQUEST_URI"];
          	foreach( $sites as $k => $v ){
          			if( $k==$current ){
          				if( is_numeric($v) ){
          					$obj = $modx->getDocumentObject('id', $v);
          					$url = $siteurl.$obj['alias'].$modx->config['friendly_url_suffix'];
          				}else{
          					$url = $v;
          				}
          				$this->sendRedirect($url, 0, 'REDIRECT_HEADER', 'HTTP/1.1 301 Moved Permanently');
          				exit;
          			}
          	}	
          }

            Jason Bird
            Twitter: @jasonabird @threeeyedbird