diff --git a/lms/templates/login.html b/lms/templates/login.html
index 85d0eb4ae1..6670738af9 100644
--- a/lms/templates/login.html
+++ b/lms/templates/login.html
@@ -51,7 +51,11 @@
$('#login-form').on('ajax:success', function(event, json, xhr) {
if(json.success) {
var u=decodeURI(window.location.search);
- var next=decodeURIComponent(u.split("next=")[1]);
+ var next = u.split("next=")[1];
+ if (next != undefined) {
+ // if next is undefined, decodeURI returns "undefined" causing a bad redirect.
+ next = decodeURI(next);
+ }
if (next && !isExternal(next)) {
location.href=next;
} else if(json.redirect_url){