2

I am a newbie to Drupal and having hard time to understand how to redirect all URLs for all terms in a specific vocabulary to custom URLs.

I have tried to use Entity Path, but it did not work for me. By some reasons this module sets automatic alias field empty in a term edit form. This breaks Pretty Path module that I am also using.

I have also tried to implement custom hook as described here, but cannot understand how to make it work only for a specific vocabulary, the hook applies to all vocabularies on my site.

I would be greatly appreciate if someone can provide a step-by-step instruction to solve this issue.

edit after I tried to get it to work via Rues

Here is exported Rules

{ "rules_redirect_an_old_path_to_a_new_path" : {
    "LABEL" : "Redirect Categories Terms to Custom Path",
    "PLUGIN" : "reaction rule",
    "ACTIVE" : false,
    "OWNER" : "rules",
    "REQUIRES" : [ "rules" ],
    "ON" : { "init" : [] },
    "IF" : [
      { "text_matches" : { "text" : [ "site:current-page:url" ], "match" : "existing_vocabulary_alias/[term:name]" } }
    ],
    "DO" : [ { "redirect" : { "url" : "custom_text\/custom_text\/[term:name]" } } ]
  }
}
2
  • What are the paths (patterns) for which you want to perform such redirect? Commented Nov 13, 2015 at 19:44
  • Pattern is as follow: Taxonmy is set to have default path in pathauto module. I want to redirect all terms in one vocabulary to root_directory/custom_path/custom_path/term_alias
    – SaidAkh
    Commented Nov 14, 2015 at 2:44

1 Answer 1

1

The Rules module is your friend to implement such redirects ...

Here is the rule you'd need for it (in Rules export format, just use copy/paste it to import it in your own setup via the Rules UI):

{ "rules_redirect_an_old_path_to_a_new_path" : {
    "LABEL" : "Redirect an old path to a new path",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules" ],
    "ON" : { "init" : [] },
    "IF" : [
      { "text_matches" : {
          "text" : [ "site:current-page:url" ],
          "match" : "the_old_path",
          "operation" : "starts"
        }
      }
    ],
    "DO" : [
      { "drupal_message" : { "message" : "Sorry, the old path (= /the_old_path) is no longer in use, you will be redirected automatically to its corresponding path (= /the_new_path)" } },
      { "redirect" : { "url" : "the_new_path" } }
    ]
  }
}

A few more tweaks to make it fit your exact scenario:

  • change all occurences of the_old_path to your old path corresponding to [term:vocabulary]/[term:name].
  • change all occurences of the_new_path to your new path corresponding to CUSTOM_WORDS/CUSTOM_WORDS/[term:name].

Attention (1): when applying those 2 changes above, you want to do so using the Views UI. So that you are sure that you'll use the exact Rules syntax, e.g. using "tokens" that the Rules module makes available. But also know that sometimes it's a bit tough and/or tricky to get that right.

Attention (2): it could well be that the Rules condition (with that text_matches) has to be replaced by something using a "regular expression" (instead of that starts in my sample).

Whenever such redirect actually happens, the user will also receive an informational message like so:

Sorry, the old path (= /the_old_path) is no longer in use, you will be redirected automatically to its corresponding path (= /the_new_path)

If you don't want such message to appear, then just delete the line containing drupal_message in the exported rule above.

7
  • Thank you Pierre.Vriens. Sorry for bothering you. Can you please give an instruction for how to set an appropriate rule for taxonomy redirect. It is still hard for me to use Rules
    – SaidAkh
    Commented Nov 14, 2015 at 5:17
  • You're not "bothering" (don't worry) ... I'd be happy to give you an exact answer if you could give me an actual example of an "old path" (to be redirected) and a "new path" (to where it should be redirected). If it applies in your case, also indicate which "patterns" such paths include, like node IDs (= nid), etc, if any. OK? Commented Nov 14, 2015 at 10:57
  • Thank you again Pierre.Vriens for being so gentle. My Old Path is set in Pathauto Module like [term:vocabulary]/[term:name]. I want to redirect to a new path like CUSTOM_WORDS/CUSTOM_WORDS/[term:name]
    – SaidAkh
    Commented Nov 14, 2015 at 11:54
  • With your help Pierre.Vriens, Rules now redirects to CUSTOM_WORDS/CUSTOM_WORDS, however it still not adds [term:name] to the url. I think it happens because Rules is not aware of [term:name] in the CONDITIONS of the Rules's settings. Is that correct?
    – SaidAkh
    Commented Nov 14, 2015 at 17:21
  • 1
    Didn't know rules could do redirection, nifty!
    – No Sssweat
    Commented Dec 22, 2015 at 20:08

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.