3

This is in connection to drupal.org issue #432384.

That's when I happen to request for page "node/1/aaa/bbbb", I'm served the response for "node/1" (it's parent page).

I can understand, this is the way Drupal menu system has been designed and works for some obvious reasons.

But I'm in a need to change this. Reason we have some aliased urls across the site, links from third party sites are taking (visitors and crawlers) to not existing page for which ideally http 404 response code and page not found to be returned. Instead Drupal showing it's parent page (with http res. code 200). This is leading to duplicate page in Google webmasters and misleading our genuine users.

I have found a patch to this issue. However it is working fine only for 80% of cases, some valid urls (admin/*, sites/default/files/ * , system/ajax/*, etc.,) leads to 404 page.

Would like to hear how others are dealing with this duplicate content issue concerning SEO and possible fix!

1

You can always make decisions about what page should be rendered based on the request path when Drupal is initializing.

It is possible using hook_init().

Implementation

For your case you could implement hook_init() in a custom module and use drupal_not_found() function as following:

<?php

/**
 * Implements hook_init().
 */
function YOURMODULE_init() {
    // Extract different variables that Drupal utilizes for routing 
    $menu_item = menu_get_item();
    $path = $menu_item['path'];     // node/%
    $href = $menu_item['href'];     // node/1
    $current_path = current_path(); // node/1/broken

    // Check if the actual path ($current_path) is different than the Drupal assumption ($href)
    if ($href != $current_path) {
        // Redirect user to 404 page which will hanlde the HTTP response as well
        drupal_not_found();
    }
}

In this code it is assumed we are browsing http://website/node/1/broken that would normally display the content of http://website/node/1.

Note

You need to know that hook_init() implementations would not always be called. (like when the page is cached). But in your case that would not cause any problem as with the same path you will always expect the same behavior.

  • thanks for your answer. Code suggested is breaking Views and Panels administration pages. – Sivaji May 13 '15 at 8:36
  • I used the same code, but one issue is coming in the same page 'page not found' repeating 2 times, please suggest how to fix this – Sachin Tonte Sep 25 '15 at 10:28
  • because i am using drupal_not_found – Sachin Tonte Sep 25 '15 at 10:35
  • @SachinTonte it seems you will need to die(); after drupal_not_found() in my code sample. Could you give it a test and let me know of result? – Alexar Nov 3 '15 at 0:01
  • 1
    Don't use die use drupal_exit() as it allows Drupal to exit cleanly. – Pobtastic Jul 21 '16 at 12:32
2

Considering the comments of the previous answer:

  • Only act on 'node' pages (aliased and not).
  • Avoid 'page not found' repeating 2 times.

Furthermore removed the unused variable $path.

The code below also avoids a 404 for the path 'node/add'. In case you use the Entity connect module that path might be followed by a dynamic token. The URL could then look like 'node/add/page/entityconnect-form-hmcOTIsupNLNBI-cZ-sSvjsbFISJeOfzT0VBpg0Sbpc'. Obviously that is a path not "known" by Drupal that would otherwise incorrectly result in a 404.

/**
 * Implements hook_init().
 */
function YOURMODULE_init() {
  // Only act on 'node' pages (aliased and not) but not on 'node/add'.
  if (arg(0) === 'node' && arg(1) !== 'add') {
    // Extract different variables that Drupal utilizes for routing.
    $menu_item = menu_get_item();
    $href = $menu_item['href'];     // node/1
    $current_path = current_path(); // node/1/broken

    // Check if the actual path ($current_path) is different than the Drupal assumption ($href).
    if ($href != $current_path) {
      // Redirect user to 404 page which will handle the HTTP response as well.
      drupal_not_found();
      // Avoid 'page not found' repeating 2 times.
      drupal_exit();
    }
  }
}

UPDATE: There is now a Drupal 7 module Force 404 that solves just this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.