diff --git a/r4032login.module b/r4032login.module
index 20cb5c5..4868cbb 100644
--- a/r4032login.module
+++ b/r4032login.module
@@ -63,6 +63,35 @@ function r4032login_form_system_site_information_settings_alter(&$form, &$form_s
     '#description' => t('The path to the user login form. Omit the beginning slash, ie: user/login'),
     '#default_value' => variable_get('r4032login_user_login_path', 'user/login'),
   );
+  $options = array('301' => '301 Moved Permanently', '302' => '302 Found');
+  $form['error_page']['r4032login_default_redirect_code'] = array(
+    '#type' => 'select',
+    '#weight' => 9,
+    '#title' => t("HTTP default redirect code"),
+    '#description' => t('The redirect code to send by default. 301 responses may be cached by browsers and proxies.'),
+    '#options' => $options,
+    '#default_value' => variable_get('r4032login_default_redirect_code', 302),
+  );
+  $form['error_page']['matching_paths'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('Redirect code for matching pages'),
+    '#collapsible' => TRUE,
+    '#collapsed' => TRUE,
+    '#weight' => 10,
+  );
+  $form['error_page']['matching_paths']['r4032login_match_redirect_code'] = array(
+    '#type' => 'select',
+    '#title' => t("HTTP redirect code to use for matching pages"),
+    '#description' => t('The redirect code to send for paths that match. 301 responses may be cached by browsers and proxies.'),
+    '#options' => $options,
+    '#default_value' => variable_get('r4032login_match_redirect_code', 301),
+  );
+  $form['error_page']['matching_paths']['r4032login_match_redirect_pages'] = array(
+    '#type' => 'textarea',
+    '#title' => '<span class="element-invisible">' . t('Only the listed pages') . '</span>',
+    '#default_value' => variable_get('r4032login_match_redirect_pages', ''),
+    '#description' => t("Specify pages by using their paths. Enter one path per line. The '*' character is a wildcard. Example paths are %blog for the blog page and %blog-wildcard for every personal blog. %front is the front page.", array('%blog' => 'blog', '%blog-wildcard' => 'blog/*', '%front' => '<front>')),
+  );
   return system_settings_form($form);
 }
 
@@ -93,7 +122,22 @@ function r4032login_redirect() {
     }
     // using drupal_goto() with destination set causes a recursive redirect loop
     $login_path = variable_get('r4032login_user_login_path', 'user/login');
-    header('Location: ' . url($login_path, array('query' => drupal_get_destination(), 'absolute' => TRUE)), TRUE, 301);
+    $page_match = FALSE;
+    $pages = variable_get('r4032login_match_redirect_pages', '');
+    if ($pages) {
+      // When on an access diend page, Drupal stores the original path in
+      // $_GET['destination'] in drupal_deliver_html_page().
+      // Convert the Drupal path to lowercase
+      $path = drupal_strtolower(drupal_get_path_alias($_GET['destination']));
+      // Compare the lowercase internal and lowercase path alias (if any).
+      $page_match = drupal_match_path($path, $pages);
+      if ($path != $_GET['destination']) {
+        $page_match = $page_match || drupal_match_path($_GET['destination'], $pages);
+      }
+
+    }
+    $code = $page_match ? variable_get('r4032login_match_redirect_code', 301) : variable_get('r4032login_default_redirect_code', 302);
+    header('Location: ' . url($login_path, array('query' => drupal_get_destination(), 'absolute' => TRUE)), TRUE, $code);
     drupal_exit();
   }
   else {
