1

When I create a node with "This is sample title" as title, the Pathauto module will create "this-is-sample-title" as URL alias and some "node/123" as real node URL. But we can browse the same content with a different URL such as

SERVER/this-is-sample-title
SERVER/this-is-sample-title/some-garbage-here

In node head source code canonical url is set to "/this-is-sample-title" and shortlink to "node/123". How do I make Drupal strict on the URL alias and redirect to 404 if URL garbage found (even if first URL segment is correct)?

3
  • That's weird. Just tested a few of my local sites and they all behave exactly as you are asking for here, without doing any special configuration. Some have Pathauto installed, some don't. Commented Nov 15, 2013 at 11:45
  • On most projects created by me it's default behavior that it redirect to 404. This problem exist on one big legacy project where I can't just have fun with module juggling. Commented Nov 15, 2013 at 14:51
  • 1
    So it could be a contrib module (and we don't know which ones are installed) or possibly some custom code written by someone else that's to blame. I think answering this may be a tall order. Commented Nov 15, 2013 at 15:16

1 Answer 1

2

You can use the url_inbound_alter hook to verify if the internal path of the request matches the used url alias. If not, trigger a 404.

function YOURMODULE_url_inbound_alter(&$path, $original_path, $path_language)
{   
    if ($path === $original_path) {
        // path is same as url alias - don't do anything
        return false;
    }

    $alias = drupal_get_path_alias($original_path);
    if ($alias && $alias === $original_path) {
        // alias is same as url alias - don't do anything
        return false;
    }

    drupal_not_found();
    drupal_exit();
}

I've tested on some projects which are using the most common contrib modules (Pathauto, Globalredirect, Views, Sitemapxml, Wysisywyg, ...), and I don't have the problem. Url alias verification is strict and returns 404 for non existing aliases. But I'm able to reproduce it with custom menu callbacks (custom code or views).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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