This is a short instruction about how to set a '410 Gone' HTTP-header for unpublished documents. This comes in handy for resources that were publsihed before and indexed by Google. If you unpublish it, MODX will tell Google that the page is gone. This better that using the 404 Not Found. More info about properly using 410 pages here:
http://searchenginewatch.com/sew/how-to/2340728/matt-cutts-on-how-google-handles-404-410-status-codes and here
https://yoast.com/deleting-pages-from-your-site/.
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.
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
}
- 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
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-requests against that list. But that's too complicated for now, and could mess up when a alias is reused elsewhere.
When you are also using the redirector extra, you need to modify the event priority of the plugin 'redirector' as follows:
onDocFormRender > 2
onDocFormSave > 3
OnPageNotFound > 4
Then, set the priority of the plugin 'set410' onPageNotFound to 5.
Enjoy!
Update: I've updated the code because the old code I've posted here didn't work with subfolders. Now, the request_uri animals/dogs/chihuahua is stripped down to chihuahua.
[ed. note: michelle84 last edited this post 10 years, 7 months ago.]