diff --git a/gogs.go b/gogs.go
index 0880650d7..b886bb7d1 100644
--- a/gogs.go
+++ b/gogs.go
@@ -16,7 +16,7 @@ import (
 	"github.com/gogits/gogs/pkg/setting"
 )
 
-const APP_VER = "0.11.30.1014"
+const APP_VER = "0.11.31.1022"
 
 func init() {
 	setting.AppVer = APP_VER
diff --git a/routes/user/auth.go b/routes/user/auth.go
index 34fdbd858..b145fb1f4 100644
--- a/routes/user/auth.go
+++ b/routes/user/auth.go
@@ -95,7 +95,6 @@ func Login(c *context.Context) {
 	} else {
 		redirectTo, _ = url.QueryUnescape(c.GetCookie("redirect_to"))
 	}
-	c.SetCookie("redirect_to", "", -1, setting.AppSubURL)
 
 	if isSucceed {
 		if isValidRedirect(redirectTo) {
@@ -103,6 +102,7 @@ func Login(c *context.Context) {
 		} else {
 			c.Redirect(setting.AppSubURL + "/")
 		}
+		c.SetCookie("redirect_to", "", -1, setting.AppSubURL)
 		return
 	}
 
diff --git a/templates/.VERSION b/templates/.VERSION
index 382fe183b..db4020828 100644
--- a/templates/.VERSION
+++ b/templates/.VERSION
@@ -1 +1 @@
-0.11.30.1014
\ No newline at end of file
+0.11.31.1022
\ No newline at end of file