Create redirect with redirect module

Public

Import redirect url

Get raw version
php
  1. function import_redirects() {
  2.  
  3. $redirect = new stdClass();
  4.  
  5. redirect_object_prepare(
  6. $redirect,
  7. array(
  8. 'source' => 'from/url',
  9. 'source_options' => array(),
  10. 'redirect' => 'to/url',
  11. 'redirect_options' => array(),
  12. 'language' => LANGUAGE_NONE,
  13. )
  14. );
  15. redirect_save($redirect);
  16.  
  17. }

From array

Get raw version
php
  1. function import_redirects() {
  2. $redirects = array(
  3. 'from/url'=>'to/url',
  4. 'from/url2'=>'to/url2',
  5. );
  6.  
  7. foreach ($redirects as $from_url => $to_url) {
  8. $redirect = new stdClass();
  9.  
  10. redirect_object_prepare(
  11. $redirect,
  12. array(
  13. 'source' => $from_url,
  14. 'source_options' => array(),
  15. 'redirect' => $to_url,
  16. 'redirect_options' => array(),
  17. 'language' => LANGUAGE_NONE,
  18. )
  19. );
  20. redirect_save($redirect);
  21. }
  22. }

Comments

Willem's picture

You can even go shorter

  1. $departments = array(
  2. array('source' => 'zutphen', 'redirect' => 'afdeling/zutphen-warnsveld'),
  3. array('source' => 'zwolle', 'redirect' => 'afdeling/zwolle'),
  4. );
  5.  
  6. // Make redirects for all departments.
  7. foreach ($departments as $department) {
  8. $redirect = new stdClass();
  9. redirect_object_prepare($redirect, $department);
  10. redirect_save($redirect);
  11. }