diff --git a/includes/locale.inc b/includes/locale.inc
index c168da0..cc430aa 100644
--- a/includes/locale.inc
+++ b/includes/locale.inc
@@ -457,6 +457,14 @@ function locale_language_url_rewrite_url(&$path, &$options) {
               $options['base_url'] = str_replace('https://', 'http://', $options['base_url']);
             }
           }
+
+          // HTTP_HOST will optionally contain the port. Preserve that for
+          // the domain of the target language as well.
+          $http_host = $_SERVER['HTTP_HOST'];
+          if (strpos($http_host, ':') !== FALSE) {
+            $http_host_tmp = explode(':', $http_host);
+            $options['base_url'] .= ':' . end($http_host_tmp);
+          }
         }
         break;
 
diff --git a/modules/locale/locale.test b/modules/locale/locale.test
index afad99f..495a2f8 100644
--- a/modules/locale/locale.test
+++ b/modules/locale/locale.test
@@ -2623,6 +2623,45 @@ class LocaleUrlRewritingTest extends DrupalWebTestCase {
     $this->drupalGet("$prefix/$path");
     $this->assertResponse(404, $message2);
   }
+
+  /**
+   * Check URL rewriting when using a domain name and a non-standard port.
+   */
+  function testDomainNameNegotiationPort() {
+    $language_domain = 'example.fr';
+    $edit = array(
+      'locale_language_negotiation_url_part' => 1,
+    );
+    $this->drupalPost('admin/config/regional/language/configure/url', $edit, t('Save configuration'));
+    $edit = array(
+      'prefix' => '',
+      'domain' => $language_domain
+    );
+    $this->drupalPost('admin/config/regional/language/edit/fr', $edit, t('Save language'));
+
+    // Enable domain configuration.
+    variable_set('locale_language_negotiation_url_part', LOCALE_LANGUAGE_NEGOTIATION_URL_DOMAIN);
+
+    // Reset static caching.
+    drupal_static_reset('language_list');
+    drupal_static_reset('language_url_outbound_alter');
+    drupal_static_reset('language_url_rewrite_url');
+
+    // Remember current HTTP_HOST.
+    $http_host = $_SERVER['HTTP_HOST'];
+    // Fake a different port.
+    $_SERVER['HTTP_HOST'] .= ':88';
+
+    // Create an absolute French link.
+    $languages = language_list();
+    $language = $languages['fr'];
+    $url = url('', array('absolute' => TRUE, 'language' => $language));
+
+    $this->assertTrue(strcmp($url, 'http://example.fr:88/') == 0, 'The right port is used.');
+
+    // Restore HTTP_HOST.
+    $_SERVER['HTTP_HOST'] = $http_host;
+  }
 }
 
 /**
