Index: src/wp-includes/functions.php
===================================================================
--- src/wp-includes/functions.php	(revision 38147)
+++ src/wp-includes/functions.php	(working copy)
@@ -4154,7 +4154,7 @@

 		// The request is for the admin
 		if ( strpos( $_SERVER['REQUEST_URI'], 'wp-admin' ) !== false || strpos( $_SERVER['REQUEST_URI'], 'wp-login.php' ) !== false ) {
-			$path = preg_replace( '#/(wp-admin/.*|wp-login.php)#i', '', $_SERVER['REQUEST_URI'] );
+			$path = preg_replace( '#/(wp-admin/?.*|wp-login\.php.*)#i', '', $_SERVER['REQUEST_URI'] );

 		// The request is for a file in ABSPATH
 		} elseif ( $script_filename_dir . '/' == $abspath_fix ) {
Index: tests/phpunit/tests/includes/helpers.php
===================================================================
--- tests/phpunit/tests/includes/helpers.php	(revision 38147)
+++ tests/phpunit/tests/includes/helpers.php	(working copy)
@@ -212,4 +212,26 @@
 	public function test_die_handler_should_handle_wp_error() {
 		wp_die( new WP_Error( 'test', 'test' ) );
 	}
+
+
+	function data_guessUrlTestCases() {
+                return array(
+                        ['wp-admin'],
+			['wp-admin/'],
+			['wp-admin/?foo=bar'],
+			['wp-login.php'],
+			['wp-login.php?foo=bar']
+		);
+	}
+
+	/**
+	 * @dataProvider data_guessUrlTestCases
+         * @ticket 36827
+         */
+	public function test_guessUrlShouldHandleEdgecases( $url ) {
+		$siteurl = site_url();
+		$this->go_to( site_url( $url ) );
+		$this->assertEquals($siteurl, wp_guess_url());
+	}
+
 }
