1

MODX redirects all not found pages to a 404 status code. Pages that are gone forever need to be set with a 410. Google Webmaster constantly checks for non existing pages in Crawl Errors with 404 responses.

Setting a 410 in .htaccess still results in a 404 by MODX

RewriteEngine On
RewriteBase /
RewriteRule ^folder/ - [G,L]

How can a 410 code be set up in MODX?

MCSharp
  • 1,068
  • 1
  • 14
  • 37

2 Answers2

0

It looks like you should be able to use the sendRedirect method:

http://rtfm.modx.com/revolution/2.x/developing-in-modx/other-development-resources/class-reference/modx/modx.sendredirect

but the interesting part is: How are you going to determine if a page is actually 410 or legitimately 404? You are probably going to have to do some research into how modx handles the 404 & try to catch it before it sends that in a plugin.

Sean Kimball
  • 4,506
  • 9
  • 42
  • 73
  • I will specifically set several directories as 410 in .htaccess. Problem is, MODX still sets them as 404. Not sure where to call the `sendRedirect` method. Seems like a snippet would need to be inserted into every single page. – MCSharp May 12 '15 at 19:43
  • Also, `sendRedirect` does not redirect to a 410. The documentation specifies 302 and 301. 410 results in 302 with the `'responseCode' => 'HTTP/1.1 410 Gone'` – MCSharp May 12 '15 at 20:09
  • the way I read that is that it just ~defaults~ to 301, it indicates you can specify your response code as part of the options array. You would best setup a plugin on a system event http://rtfm.modx.com/revolution/2.x/developing-in-modx/basic-development/plugins/system-events - after the request is made, but before the page is rendered & response delivered. not sure which one would be most appropriate - you will have to test. – Sean Kimball May 12 '15 at 20:25
0

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
Vierkante Meter
  • 105
  • 1
  • 9
  • This will set ALL 404 pages to 410 and is not the desired effect. – MCSharp Nov 18 '15 at 01:57
  • That's not correct. This script will check if the requested alias existst, and if so, only then it will send you to the 410. – Vierkante Meter Nov 18 '15 at 08:41
  • Looks like an interesting solution, but it does not work, at least not for me on MODX 2.7.3. Attached it to the event OnPageNotFound and replaced 411 with my 404 page id, but any random page name still returns a 404. Unsure if I have overlooked something as I cannot find out if the plugin even triggers. – AndreasT Aug 09 '20 at 17:46