diff --git a/redirect.module b/redirect.module
index 993eeed..5eed502 100644
--- a/redirect.module
+++ b/redirect.module
@@ -559,22 +559,44 @@ function redirect_fetch_rids_by_path($source, $language, $enabled_only = FALSE)
   // Run a case-insensitive query for matching RIDs first.
   $rid_query = db_select('redirect');
   $rid_query->addField('redirect', 'rid');
-  // Prevent errors if redirect_update_7101() has not yet been run.
-  if ($enabled_only && db_field_exists('redirect', 'status')) {
+  $rid_query->addField('redirect', 'source');
+
+  // Add wildcard search to the query.
+  $wildcards = array();
+  $source_elements = explode('/', $source);
+  $length = count($source_elements);
+  // If the source contains only one element there is nothing to wildcard.
+  if ($length > 1) {
+    // Add the original url.
+    $wildcards[] = $source;
+    // Generate the wildcard searches, with minimal path string.
+    // Don't use the last element from the source_array.
+    for ($i = $length - 1; $i > 0; $i--) {
+      $wildcards[] = implode('/', array_slice($source_elements, 0, $i)) . '/*';
+    }
+  }
+
+  if ($enabled_only) {
     $rid_query->condition('status', 1);
   }
+
   if ($source != variable_get('site_frontpage', 'node')) {
-    $rid_query->condition('source', db_like($source), 'LIKE');
+    if (empty($wildcards)) {
+      $rid_query->condition('source', db_like($source), 'LIKE');
+    }
+    else {
+      $rid_query->condition('source', $wildcards, 'IN');
+      // Order results descending because the longest value will be the most
+      // specific match to the source.
+      $rid_query->orderBy('source', 'DESC');
+    }
   }
   else {
-    $source_condition = db_or();
-    $source_condition->condition('source', db_like($source), 'LIKE');
-    $source_condition->condition('source', '');
-    $rid_query->condition($source_condition);
+    $rid_query->condition('source', array($source, ''), 'IN');
   }
   $rid_query->condition('language', array($language, LANGUAGE_NONE));
-  $rids = $rid_query->execute()->fetchCol();
-  return $rids;
+
+  return $rid_query->execute()->fetchCol();
 }
 
 
