It would be great if there was an API call to add redirects.

Other modules that are importing content from another CMS could use this API to automatically setup all the necessary redirects.

Comments

mdlueck’s picture

subscribe

We are working through several conversions from static HTML to Drupal. I have been utilizing redirects in the .htaccess file, but something needs to remain on disk else it invokes the:

  # Rewrite current-style URLs of the form 'index.php?q=x'.
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

rule and produces a really ugly URL.

With SSH access to the server, touch produces 0 length files easily, and mkdir directories likewise (assuming the directories from the old site have been deleted...)

An elegant way of handling Drupal migrations would be much appreciated.

HorsePunchKid’s picture

I'm not sure this will fit the bill, but there's already a relatively convenient (but undocumented) path_redirect_save function that takes a $form_values array and saves it to the database. That array looks something like:

 $form_values = array(
  'path' => 'your/source/path',
  'redirect' => 'destination/path/or/url',
  'query' => 'optional=query&string=values',
  'fragment' => 'optional-internal-anchor',
  'type' => 302,
);
path_redirect_save($form_values);

I haven't tested it, but something roughly like that could work.

HorsePunchKid’s picture

If you need something syntactically more convenient, we could add something like:

function path_redirect_create($path, $redirect, $query = '', $fragment = '', $type = 301) {
  return path_redirect_save(array(
    'path' => $path,
    'redirect' => $redirect,
    'query' => $query,
    'fragment' => $fragment,
    'type' => $type,
  ));
}

This would let you just call:

path_redirect_create('your/source/path', 'destination/or/url');

We could also have path_redirect_save set default values for the array you pass it; that would also reduce the code required to call it. I'm leaning toward this latter strategy. The same line would look like:

path_redirect_save(array('path' => 'your/source/path', 'redirect' => 'destination/or/url'));

HorsePunchKid’s picture

Version: 5.x-1.1-beta1 » 5.x-1.x-dev
Status: Active » Reviewed & tested by the community
StatusFileSize
new1.33 KB

The last suggestion would works with this patch. Good enough?

HorsePunchKid’s picture

Status: Reviewed & tested by the community » Fixed

I committed something more or less like this, only I factored out the default redirect type as a define. Also, the default value for the argument to path_redirect_save was not actually useful. You do have to pass it something, after all.

Anonymous’s picture

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for two weeks with no activity.