0

How can I best redirect the following in Drupal 7?

stuff/bookA.pdf
stuff/bookB.pdf
stuff/magA.pdf
stuff/magB.pdf
stuff/...
stuff/magn.pdf

to

books/booka.pdf
books/bookb.pdf
mags/maga.pdf
mags/magb.pdf
mags/...
mags/magn.pdf

There are only a few Books (which can be redirected individually). There are hundreds of Mags.

  • I cannot find a module which does this.
  • I could write a custom module which checks the URL and redirects along these lines.
  • I am lost writing redirects in .htaccess.

Is there a noticeable performance improvement using .htaccess over the module solution?

If using .htaccess what would the rules be?

1 Answer 1

0

If you directly edit .htaccess (at the root directory of Drupal), I suppose you may as well be very careful, because any update of Drupal may delete it. For that reason, as I understand it is not recommended in general.

I have been using Redirect module for that purpose and am content with it. With the module, you can manually set the redirection for each path in its configuration panel. But as you have hundreds of paths, perhaps you want a programmatical way. An article by Dale McGladdery is concise and helpful for how to do that, using the module (It uses the Migration module; in your case ignore that part and set "From" and "To" URLs to suit your requirements). Basically, you use redirect_save($redirect) function, where $redirect is the stdClass object, definining the paths and type of redirection.

A potential trouble in his example is, if the same redirection exists, it raises an error. Therefore if you ever rerun the process to set up the redirections, you will have a trouble. To overwrite the existing redirection, you need to appropriately set rid of the object to pass to the funciton redirect_save(). I have done that in my migration, and you can have a look at the source code at GitHub (complete() section), if you want to have a look at an example.

2
  • Thanks for your response. I now have a question which betrays my lack of understanding of redirects. I need to manage the move of these files best for search engines. I believe this means 301? Presumably a self coded custom module (as I proposed above) would not be optimum (because it just reroutes internally without 301)? How does the Redirect module work? Does it return a 301 (or just reroute internally like my proposed custom module)? I hope I have been clear about my confusion!
    – Polly
    Nov 2, 2014 at 16:15
  • In the Redirect module, it is configurable (at /admin/config/search/redirect/settings), but the default is HTTP 301. To my best knowledge, that (301) is the best way for the search engines because when they see 301, they reregister their record of the path. Nov 2, 2014 at 16:30

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.