diff --git a/classes/urlhelper.php b/classes/urlhelper.php
index 29c81e760efbe3e075bd3f3ceeaf52ba640606fb..c8e87c8ae6a5f388565419deab76533bee8b752c 100644
--- a/classes/urlhelper.php
+++ b/classes/urlhelper.php
@@ -69,6 +69,13 @@ class UrlHelper {
 			$tokens['path'] = implode("/", array_map("rawurlencode", explode("/", $tokens['path'])));
 		}
 
+		//convert IDNA hostname to punycode if possible
+		if (function_exists("idn_to_ascii")) {
+			if (mb_detect_encoding($tokens['host']) != 'ASCII') {
+				$tokens['host'] = idn_to_ascii($tokens['host']);
+			}
+		}
+
 		$url = self::build_url($tokens);
 
 		if (filter_var($url, FILTER_VALIDATE_URL) === false)
@@ -82,14 +89,6 @@ class UrlHelper {
 				return false;
 		}
 
-		//convert IDNA hostname to punycode if possible
-		if (function_exists("idn_to_ascii")) {
-			if (mb_detect_encoding($tokens['host']) != 'ASCII') {
-				$tokens['host'] = idn_to_ascii($tokens['host']);
-				$url = self::build_url($tokens);
-			}
-		}
-
 		return $url;
 	}