Is it possible to rewrite parts of taxonomy terms URL custom aliases before output, for example with something similar to the the following code?

  function replace_term_url ($term_alias) {
    // Url alias "article/first"
    $term_alias = str_replace("article", "article-sub", $term_alias);

In Drupal 7 there isn't a specific hook to alter the URL associated with a taxonomy term, but you can use the generic hooks for altering URLs, which are hook_url_inbound_alter(), and hook_url_outbound_alter().

function mymodule_url_outbound_alter(&$path, &$options, $original_path) {
  if (preg_match('|^taxonomy/term/([0-9]*)|', $path, $matches)) {
    // Alter $path; $matches[1] contains the term ID.

function mymodule_url_inbound_alter(&$path, $original_path, $path_language) {
  // Revert the change done in mymodule_url_outbound_alter().

hook_url_outbound_alter() is invoked for URLs output from Drupal, hook_url_inbound_alter() is invoked for URLs Drupal is receiving.

In Drupal 6, it is possible to implement hook_term_path(), but that is invoked only for the module associated with the taxonomy term. The code invoking that hook is in taxonomy_term_path().

  if ($vocabulary->module != 'taxonomy' && $path = module_invoke($vocabulary->module, 'term_path', $term)) {
    return $path;

If you want to use a third-party module, then there is the Pathauto module, which allows to automatically set the alias for users, nodes, and taxonomy terms.

The Pathauto module automatically generates URL/path aliases for various kinds of content (nodes, taxonomy terms, users) without requiring the user to manually specify the path alias. This allows you to have URL aliases like /category/my-node-title instead of /node/123. The aliases are based upon a "pattern" system that uses tokens which the administrator can change.

  • Hi kiamlaluno, thx for your answer. i know "Pathauto" and url_inbound_alter / url_outbound_alter. But with ...alters it's possible to get an original path ( taxonomy/term/%, etc.) and not alias (category/cars, etc.). I need to replace or delete parts of alias path. – QQQ Mar 17 '12 at 18:24
  • If you implement both the hooks, so that pathA is replaced with pathB in hook_url_outbound_alter(), and pathB is replaced with pathA in hook_url_inbound_alter(), then it is like having a path alias. What I reported are the only ways I know of. – apaderno Mar 17 '12 at 18:41

You should be able to do that with the Pathauto module:

You can use it to set up automatic aliases for taxonomy terms, as well as content and other things.

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.