pepebe
7/7/2015 - 7:30 AM

redirect.plugin.php

<?php
/*
Redirect OnPageNotFOund an unknown resource to another document */

$msg = array();
$success = false;

/* array( 'old/path/witout/trailing/leading/slashes', new_id ) */

$urls = array(
     array('de/kundenservice/dokumente-und-downloads/preisliste' , 738)
    ,array('de' , 2)
);

if ($modx->event->name == 'OnPageNotFound') {

    $request_url  = parse_url($_SERVER['REQUEST_URI']);
    $path = trim($request_url['path'],'/');

    foreach($urls as $url){
        if($url[0] == $path){
            $msg['msg'] = 'Rewrite rule found for: ' + $url[0];
            
        	$resource = $modx->getObject('modResource', array('id' => $url[1] ));
        	if($resource) {
        		$url = $modx->makeUrl($resource->get('id'));
        		$modx->sendRedirect($url);
        		$msg['success'] = 'Success: Request rewritten to resource: ' . $path;
        		$msg['rewrite'] = true;
        	}            
        	else {
        	    $msg['error'] = 'Error: Unknown resource: ' . $path;
        	    $msg['rewrite'] = false;
        	}
        	break;
        }
    }
    if(!$msg['msg']){
        $msg['msg'] = 'No rewrite rule for ' . $path . ' found.';
        $msg['rewrite'] = false;
    }
    $modx->log(modX::LOG_LEVEL_ERROR, '[redirector]: ' . print_r($msg,true) );
}

return '';