URL rewrite override in Drupal

Path alias configuration in Drupal 6Drupal 6 introduced a new automagical function for rewriting URLs output through most of Drupal called custom_url_rewrite_outbound().

The path module already gives you the ability to alias paths to different places, but only within the same domain. To redirect a path that has already been aliased, or to a different subdomain or different location altogether, adding custom_url_rewrite_outbound() to settings.php might be a solution for you. Since this is not a hook, it can only be used once per Drupal installation, which is part of why settings.php is a recommended place for it.

This code example will rewrite user, user/register, about, advertise, forum, and blog to other domains by changing the base_url by reference. Any URL on the site that passes through Drupal's core url() function will be rewritten, even if it was aliased by the path module.

<?php
function custom_url_rewrite_outbound(&$path, &$options, $original_path) {
 
$url_rewrites = array(
   
// Drupal core paths
   
'user' => 'http://users.mysite.com',
   
'user/register' => 'http://users.mysite.com',

   
// general information paths
   
'about' => 'http://www.mysite.info',
   
'advertise' => 'http://www.mysite.info',

   
// other
   
'forum' => 'http://forums.mysite.com',
   
'blog' => 'http://blogs.mysite.com',
  );
  while (list(
$key, $value) = each($url_rewrites)) {
    if (
$path == $key || $original_path == $key) {
     
$options['base_url'] = $value; // updated by reference
     
$options['absolute'] = TRUE;
      break;
    }
  }
}
?>


» deekayen's blog · Printer-friendly version · Send to friend Topics:

Changing default url's for security reasons

Excellent work, I can see how this can be useful for subdomains.

Now imagine if I want to change a default path from: www.example.com/admin to: www.example.com/secret-admin

..and when someone enters www.example.com/admin I want it to redirect to the front page.

A version of this for D5 can be found here, using 'custom_url_rewrite': http://www.drupalcoder.com/story/9-drupal-custom-url-rewriting-change-th...

Any ideas how to go about this in D6?

reply

Also available in D5!

For those of us still developing D5 sites, the same functionality is available as custom_url_rewrite(). It includes both the outbound and the inbound functionality, which is a bit confusing, hence the split into two functions for D6. One thing I like to do with it is substitute "article" (or "content") for "node" sitewide. I hadn't thought about using it in this particular way, though. Great tip.

reply

Good explanation. Seen this

Good explanation. Seen this in the API, but couldn't never grasp what I could actually do with it.

reply

Post new comment

  • Lines and paragraphs break automatically.
  • Allowed HTML tags: <hr /> <a> <p> <em> <strong> <cite> <code> <blockquote> <ul> <ol> <li> <dl> <dt> <dd>
  • Web page addresses and e-mail addresses turn into links automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.

More information about formatting options

By submitting this form, you accept the Mollom privacy policy.