1

I am using a Drupal site on top of nginx, and would like to be able to redirect a set of pages from an old style url, to a new one, using 301s. The content has already been migrated over to the new urls, but there may be links elsewhere with the old style. How can I achieve 301 redirects for matching paths, to redirect using part of the information from the old url to inject into the new one (namely [timestamp]).

From:

example.org/article/[timestamp]

To:

example.org/article/doi/static-[timestamp]

I know how I would do this with .htaccess, but is there a way to do this in Drupal itself, or a module to achieve this?

Having looked at the docs, I think that

drupal_goto($path = '', array $options = array(), $http_response_code = 301)

May be the best way to go, with a corresponding (pseudocode):

if path == oldpath, 
    drupal_goto(etc.)

2 Answers 2

1

Probably the Global Redirect module suits your needs.

You may have to implement the timestamp feature as a new token with a custom module.

1
  • Thanks for the answer - as it happens, Global Redirect is overkill for what I need, and so have implemented as per the answer.
    – jvc26
    Mar 22, 2012 at 21:33
1

In this case, I looked at @Arosboro's suggestion about using Global Redirect. This was a bit more than I actually required, as all I needed was a simple redirect for a particular page. What I did instead was implement a quick module which handled the redirect based on a regex match for the old URL.

function redirect_init() {
    if (isset($_GET['destination'])) {
        $destination = $_GET['destination'];
        unset($_GET['destination']);
    }

    $request_path = redirect_request_path();

    // If the request_path() matches an article, redirect to the new URL
    // for articles.
    if ($request_path) {
        drupal_goto($request_path, array(), 301);
    }
}


function redirect_request_path() {
    $path = ltrim(request_path(), '/');

    if (preg_match('/^article\/\d+$/', $path)) {
    $path_list = explode('/', $path);
    $timestamp = $path_list[1];
        return 'article/doi/10.7244/cmj-' . $timestamp;
    } else {
        return;
    }
}

Your Answer

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

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