Removing the Front Slug from Hierarchical Paths Generated by Pathauto
November 01, 2014

Generating hierarchical paths in Drupal 7 based on the pathauto module is not an easy task.

First, in a nutshell, hierarchical paths are ones that follow a certain hierarchy, a tree structure, or even called multi level or multi depth. For example, let's say we have a menu in Drupal with an About page which has 3 child menu items: Mission, Vision, and History. Additionally, let's say the History page has 2 child menu items of its own. Ideally, you'd want your paths to be hierarchical and hence look like this:

A quick search reveals that the below pathauto pattern is a good candidate. Try it and you'll see it works and properly generates hierarchical paths similar to those detailed above.


However, this comes with a plethora of problems. We'll tackle one of them in this post and others in an upcoming fully detailed post.

The issue is that if one of your menu items is a child item of a menu item that points to the front page, pathauto will generate a "front" slug and prepend it to the beginning of the generated path. For example, if you set our hypothetical About page as a child menu item of your home page, you will end up with paths similar to the below ones.<front>/about<front>/about/mission<front>/about/vision<front>/about/history<front>/about/history/old<front>/about/history/new

Those are definitely not easy to remember or user friendly in any way and even technically not usable under certain platforms depending on how your web server is configured.

Our solution was to build a Drupal 7 custom module and add the following hook function inside it. Here's a link to our tutorial on creating a custom module in Drupal and all you have to do is follow the steps and then copy/paste the function below inside the custom module you got from the tutorial.

As usual, read the comments in the function below for more info, and, enjoy coding!


 * Implements hook_pathauto_alias_alter().
 * Needed so that we remove the buggy slug <front> from paths when their
 * nodes are a sub-menu-item of the front page while using this pathauto 
 * pattern: [node:menu-link:parent:url:path]/[node:title]
 * If the parent is the homepage, all menu child entries will get <front> 
 * added to the beginning of their URL alias which looks really ugly and 
 * doesn't work on all operating systems. We need to remove it.
 * Pathauto 7.x-1.2 doesn't handle this properly. Maybe later versions will.
function sk_pathauto_alias_alter(&$alias, array &$context) {
    //Init the slashed front slug value
    $front_slug_slashed = '<front>/';
    //If the alias contains the slashed front slug, remove it
    //Note that pathauto will later check if the generated slug is a duplicate
    //and append an incrementing number to it so no need to worry about this
    if(strpos($alias, $front_slug_slashed) !== false) {
        //Save the non-modified alias for logging
        //Note: no better way to clone a string in PHP?
        $original_alias = $alias . '';
        //Remove the front slug from within the alias
        $alias = str_replace($front_slug_slashed, '', $alias);
        //Log to Drupal's watchdog as notice
        $msg = '';
        $msg .= 'Altered pathauto alias from "' . check_plain($original_alias);
        $msg .= '" to "' . check_plain($alias) . '"';
        watchdog('sk', $msg, array(), WATCHDOG_NOTICE);

Drupal Development Open Source SEO

Share this post

Written by
Mario Awad

Founder of SOFTKUBE, lead developer, and getting things done addict. Passionate about open source, user interface design, business development, and the tech world.

More about Mario Awad


A small team of experts developing simple, usable, and high-quality web solutions. We blog about business, entrepreneurship, web development, and technology.

More about us

Recent Posts

Custom Theme Migration from Drupal 9 to Drupal 10

How to Change the Most Recent Git Commit Message

How to Make Google Chrome Forget a Permanent HTTP 301 Redirect

Finding and Fixing Unintended Body Overflow to Remove Horizontal Scrollbars

View all posts

All Posts Categories

Business Cheat Sheets CLI Design Development Downloads Drupal Email Google Apps HID Keyboards Multilingualism Open Source Philosophy PHP Pointing Devices Productivity Quotes Science Security SEO Technology Thoughts Windows Zend Framework