This script checks if the requested resource exists, and if it does it returns a 410. If the resource does not exist, it returns the default 404. This comes in handy for resources that were publsihed before and indexed by Google.
This script does not check for deleted resources. If you want this, you need to write a code that stores all the aliases that ever existed, and check any OnPageNotFound-request against that list.
Create a plugin named set410
Check system event OnPageNotFound
Paste this code:
<?php
$alias = basename(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)); // Get last alias from url (preserve only the alias after last slash). Needed when using subfolders.
$resource = $modx->getObject('modResource', array('alias' => $alias));
if ($resource) {
$modx->sendForward(411, 'HTTP/1.1 410 Gone'); // ID found > 410
}
?>
Remember:
- Replace 999 with the ID of the 410 page.
- Remember to test this
function with a browser you aren't logged in the manager!
- This script is only tested with Friendly URLS turned on