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.