0

I have to make redirect for links:
http://example.com/file/file1.pdf
http://example.com/file/file2.pdf
...
to links:
http://example.com/sites/default/files/file1.pdf
http://example.com/sites/default/files/file2.pdf
...
I've tried .htaccess changing, but fail. (it's too Voodoo for me)
Redirect project has no such function (afaik).
I don't know what hook can I use in my module for such redirect.

How can I make such redirect?

UPDATE:

I've created simple .htaccess file:

  RewriteEngine on
  RewriteRule ^file/(.*)$ sites/default/files/$1 [L]

And it works.

But when I insert in original Drupal .htaccess
line RewriteRule ^file/(.*)$ sites/default/files/$1 [L] after RewriteEngine on
- there is no successful redirect.

Why?

SOLUTION:

  RewriteEngine on
  RewriteRule ^file/(.*)$ sites/default/files/$1 [L, R=301]
2

As you seem to have learned the Redirect module is really for page-specific redirects (this single URL redirects to that URL), not patterns, as your case seems to require. For this a server-side redirect is really the way to go. Not only is it a standard solution to this kind of problem, but it can happen independent of Drupal, so there is no need to bootstrap Drupal just to process the redirect.

I think the specifics of setting-up a server-side redirect a a bit are a bit beyond the scope of this space (as it's not a Drupal-specific concept), but there are lots of resources out there on the topic, and I think you'll find that it's not too hard. You mentioned that it feels like "Voodo", but I think it will prove far more straightforward than a module-based solution implemented in php. Here's one example stackexchange post that might be relevant to your case.

All that said, because your question does ask for a moudle-specific way to do it, here are some notes on that too. This is much less ideal, as noted above, but it's worth referencing. For this I think you would want to trigger a drupal_goto() inside of hook_init(). A very simple-case example of this pattern can be referenced in this post, and interpolated for your example:

/**
 * Implements hook_init().
 */
function mymodule_init() {
  $path = current_path();
  // preg_match() could also work instead of drupal_match_path()
  if (drupal_match_path($path, 'file/*')) {
    drupal_goto('sites/default/' . $path);
  }
}

I suppose there may be real use cases for this kind of module-based approach, but as I noted above, I really don't think your case is one of them. You would probably be best-served with a service-side redirect (in Apache/.htaccess).

2
  • I've updated the question. I've tried .htaccess woodoo again, but without success. Where can be problem? – Dimetry Feb 19 '15 at 19:53
  • I think you might need a flag at the end to signify a redirect ([R]). See the docs. You may also need to signify the slash (/) in your source pattern, but I'm not positive on that... RewriteRule ^/file/(.*)$ sites/default/files/$1 [L,R=301]. Anyway, these specifics might be better suited for stackoverflow.com. – rjacobs Feb 19 '15 at 20:12
0

Redirecting from one url to another You can try

https://www.drupal.org/project/path_redirect module to redirect from one URl to another

This module allows you to specify a redirect from one path to another path or an external URL, using any HTTP redirect status.

D7CX: I pledge that Path redirect will have a full Drupal 7 release on the day that Drupal 7 is released. Note that the project has moved to http://drupal.org/project/redirect for Drupal 7.

2
  • Isn't Path Redirect just the D6 version of Redirect? – rjacobs Feb 19 '15 at 19:12
  • yes you are right but if you can see the project page that specified in redirect module is also workable i ll update the answer :) – DEVARAJ JOHNSON Feb 19 '15 at 19:14

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.