2

I've run into the issue where I end up with something like this:

about-us01 --> about-us02 --> about-us

The correct path is "about-us". But due to admins changing things I have ended up in a situation where there are sometimes old redirects lingering in the system. The problem is that these google says it's not good to redirect multiple times. So the above redirect is not very effective.

Is there a way that I can get Drupal 7 to automatically skip the 'middle' redirect if it knows that is only going to result in another redirect? So instead of:

about-us01 --> about-us02 --> about-us

I want:

about-us01 --> about-us
about-us02 --> about-us

In both cases of accessing the links, it must take you directly to the about-us page and not any inbetween links if it knows the true destination?

2
  • I don't think there is an automatic away.
    – No Sssweat
    Commented Aug 1, 2016 at 7:53
  • @NoSssweat : are you sure? Commented Aug 1, 2016 at 8:07

1 Answer 1

1

Use the Rules module with a custom rule which looks like so (in Rules export format):

{ "rules_redirect_multiple_paths" : {
    "LABEL" : "Disallow multiple paths",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules" ],
    "ON" : { "init" : [] },
    "IF" : [
      { "text_matches" : {
          "text" : [ "site:current-page:path" ],
          "match" : "about-us\\d+",
          "operation" : "regex"
        }
      }
    ],
    "DO" : [
      { "drupal_message" : {
          "message" : "Redirect to path about-us ...",
          "type" : "error"
        }
      },
      { "redirect" : { "url" : "about-us" } }
    ]
  }
}

Some details about how to use read this rule:

  • Rules Event: use the "Drupal is initializing" event, to ensure that the rule gets triggered BEFORE the actual content (of a path to be redirected) is shown. If you'd use an event like "content is viewed", you'd be too late, the content "would" already be shown.
  • Rules Condition: verify if the path to be redirected matches any of the paths you want to be redirected, such as about-us01 or about-us02 ( * ).
  • Rules Actions:
    • Show a message about the redirect, which is just for QA testing (feel free to remove that Rules Action after you saw it working).
    • Perform the actual redirect to the desired page.

( * ) Note that the actual Regular Expression I used goes a bit further: it would also perform a similar redirect for paths such as about-us03 or about-us456, though it would not trigger such redirect for (eg) about-us/789. Here is an attempt to translate the various parts used in the regular expression about-us\\d+:

  • about-us: it must start with about-us.
  • \: escape character.
  • \d: it must be followed by a digit (anything from 0 to 9).
  • +: it may be followed by 1 or more of the preceding token (so one or more extra digits).

PS: If you're struggling with regular expressions, then have a look at RegExr.

4
  • Can you put in words the logic of your rule?
    – No Sssweat
    Commented Aug 1, 2016 at 8:24
  • 1
    @NoSssweat : does this help? Commented Aug 1, 2016 at 10:13
  • hmmm the only question is, will this over-rule the original redirect from module "x" ?
    – No Sssweat
    Commented Aug 1, 2016 at 10:26
  • 1
    @NoSssweat : "now" you make me doubt, a very little bit. As "Drupal is initializing" indicates, it happens pretty early, so chances are that it will indeed overrule the original redirect. But the result may also vary, based on which technique is used for those "old redirects lingering in the system". So instead of us guessing which technique it could be (actually does the OPer know?), I suggest OPer adds more details about the technique used, and also performs an appropriate experiment using the rule I suggested. Commented Aug 1, 2016 at 10:39

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.