diff -ru rc-beta2/index.php rc-beta/index.php
--- rc-beta2/index.php	2008-12-05 12:06:05.489545804 +0100
+++ rc-beta/index.php	2008-12-05 11:34:30.314621002 +0100
@@ -96,8 +96,20 @@
         $_SERVER['REMOTE_ADDR']));
     }
 
+    // re-generate old url
+    if($oldurl = get_input_value('_url', RCUBE_INPUT_POST)) {
+      $oldurl = explode('&', urldecode($oldurl));
+      foreach($oldurl as $idx => $urlpart) {
+        $part = explode('=', $urlpart);
+        $url[$part[0]] = $part[1];
+      }
+    }
+    
     // send redirect
-    $OUTPUT->redirect();
+    if(!empty($url) && $url['_action']!='logout' && $url['_task']!='logout')
+      $OUTPUT->redirect($url);
+    else
+      $OUTPUT->redirect();
   }
   else {
     $OUTPUT->show_message($IMAP->error_code < -1 ? 'imaperror' : 'loginfailed', 'warning');
diff -ru rc-beta2/program/include/rcmail.php rc-beta/program/include/rcmail.php
--- rc-beta2/program/include/rcmail.php	2008-12-05 12:06:21.582172961 +0100
+++ rc-beta/program/include/rcmail.php	2008-12-05 11:18:54.121628338 +0100
@@ -904,6 +904,9 @@
     if (!is_array($p))
       $p = array('_action' => @func_get_arg(0));
 
+    if(!$p['task'] && $p['_task'])
+      $p['task'] = $p['_task'];
+
     if (!$p['task'] || !in_array($p['task'], rcmail::$main_tasks))
       $p['task'] = $this->task;
 
diff -ru rc-beta2/program/include/rcube_template.php rc-beta/program/include/rcube_template.php
--- rc-beta2/program/include/rcube_template.php	2008-12-05 12:06:21.110138169 +0100
+++ rc-beta/program/include/rcube_template.php	2008-12-05 11:34:50.659424476 +0100
@@ -942,10 +942,17 @@
 
         $_SESSION['temp'] = true;
 
+	// save old url
+	if($oldurl = get_input_value('_url', RCUBE_INPUT_POST))
+	  $oldurl = $oldurl;
+	else
+	  $oldurl = urlencode($_SERVER['QUERY_STRING']);
+
         $input_user   = new html_inputfield(array('name' => '_user', 'id' => 'rcmloginuser', 'size' => 30) + $attrib);
         $input_pass   = new html_passwordfield(array('name' => '_pass', 'id' => 'rcmloginpwd', 'size' => 30) + $attrib);
         $input_action = new html_hiddenfield(array('name' => '_action', 'value' => 'login'));
         $input_tzone  = new html_hiddenfield(array('name' => '_timezone', 'id' => 'rcmlogintz', 'value' => '_default_'));
+        $input_oldurl = new html_hiddenfield(array('name' => '_url', 'id' => 'rcmloginurl', 'value' => $oldurl));
         $input_host   = null;
 
         if (is_array($default_host)) {
@@ -985,6 +992,7 @@
 
         $out = $input_action->show();
         $out .= $input_tzone->show();
+        $out .= $input_oldurl->show();
         $out .= $table->show();
 
         // surround html output with a form tag
