Index: typo3/sysext/cms/tslib/class.tslib_content.php
===================================================================
--- typo3/sysext/cms/tslib/class.tslib_content.php	(Revision 5240)
+++ typo3/sysext/cms/tslib/class.tslib_content.php	(Arbeitskopie)
@@ -5498,7 +5498,8 @@
 							$addQueryParams.= '&cHash='.t3lib_div::shortMD5(serialize($pA));
 						}
 
-						$tCR_domain = '';
+						$targetDomain = '';
+						$currentDomain = t3lib_div::getIndpEnv('HTTP_HOST');
 						// Mount pages are always local and never link to another domain
 						if (count($MPvarAcc))	{
 							// Add "&MP" var:
@@ -5526,33 +5527,48 @@
 								}
 							}
 
-							// This checks if the linked id is in the rootline of this site and if not it will find the domain for that ID and prefix it:
-							$tCR_rootline = $GLOBALS['TSFE']->sys_page->getRootLine($page['uid']);	// Gets rootline of linked-to page
-							$tCR_flag = 0;
-							foreach ($tCR_rootline as $tCR_data)	{
-								if ($tCR_data['uid'] == $GLOBALS['TSFE']->tmpl->rootLine[0]['uid'])	{
-									$tCR_flag = 1;	// OK, it was in rootline!
+							// find all domain records in the rootline of the target page
+							$targetPageRootline = $GLOBALS['TSFE']->sys_page->getRootLine($page['uid']);	// Gets rootline of linked-to page
+							$foundDomains = array();
+							$targetPageRootlinePids = array();
+							$isCurrentPagePartOfTargetRootline = false;
+							foreach ($targetPageRootline as $data)	{
+								if ($data['uid'] == $GLOBALS['TSFE']->id) {
+									$isCurrentPagePartOfTargetRootline = true;
 									break;
 								}
-								if ($tCR_data['is_siteroot']) {
-									// Possibly subdomain inside main domain. In any case we must stop now because site root is reached.
-									break;
-								}
+								$targetPageRootlinePids[] = intval($data['uid']);
 							}
-							if (!$tCR_flag)	{
-								foreach ($tCR_rootline as $tCR_data)	{
-									$res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('domainName', 'sys_domain', 'pid='.intval($tCR_data['uid']).' AND redirectTo=\'\''.$this->enableFields('sys_domain'), '', 'sorting');
-									$row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);
-									$GLOBALS['TYPO3_DB']->sql_free_result($res);
-									if ($row)	{
-										$tCR_domain = preg_replace('/\/$/','',$row['domainName']);
-										break;
+							// if the target page is not a subpage of the current page and not the current page itself
+							if (!$isCurrentPagePartOfTargetRootline) {
+								$res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
+									'pid, domainName',
+									'sys_domain',
+									'pid IN ('.implode(',', $targetPageRootlinePids).') ' .
+										' AND redirectTo=\'\''.$this->enableFields('sys_domain'),
+									'',
+									'sorting DESC'
+								);
+								while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
+									if (!isset($foundDomains[$row['pid']])) {
+										$foundDomains[$row['pid']] = preg_replace('/\/$/', '', $row['domainName']);
 									}
 								}
+								$GLOBALS['TYPO3_DB']->sql_free_result($res);
+							
+								// set targetDomain to first found domain record if the target page cannot be reached within the current domain
+								if (count($foundDomains) > 0 && !in_array($currentDomain, $foundDomains)) {
+									foreach ($targetPageRootlinePids as $pid) {
+										if (isset($foundDomains[$pid])) {
+											$targetDomain = $foundDomains[$pid];
+											break;
+										}
+									}
+								}
 							}
 						}
-							// If other domain, overwrite
-						if (strlen($tCR_domain) && !$enableLinksAcrossDomains) {
+							// if target page has a diffenrent domain and the current domain's linking scheme (e.g. simulateStaticDocuments/RealURL/...) should not be used
+						if (strlen($targetDomain) && !$enableLinksAcrossDomains) {
 							$target = isset($conf['extTarget']) ? $conf['extTarget'] : $GLOBALS['TSFE']->extTarget;
 							if ($conf['extTarget.']) {
 								$target = $this->stdWrap($target, $conf['extTarget.']);
@@ -5561,13 +5577,13 @@
 								$target = $forceTarget;
 							}
 							$LD['target'] = $target;
-							$this->lastTypoLinkUrl = $this->URLqMark('http://'.$tCR_domain.'/index.php?id='.$page['uid'],$addQueryParams).$sectionMark;
-						} else {	// Internal link:
+							$this->lastTypoLinkUrl = $this->URLqMark('http://'.$targetDomain.'/index.php?id='.$page['uid'],$addQueryParams).$sectionMark;
+						} else {	// Internal link or current domain's linking scheme should be used
 							if ($forceTarget) {
 								$target = $forceTarget;
 							}
 							$LD = $GLOBALS['TSFE']->tmpl->linkData($page,$target,$conf['no_cache'],'','',$addQueryParams,$theTypeP);
-							if (strlen($tCR_domain)) {
+							if (strlen($targetDomain)) {
 								// We will add domain only if URL does not have it already.
 
 								if ($enableLinksAcrossDomains) {
@@ -5581,8 +5597,8 @@
 									}
 								}
 								$urlParts = parse_url($LD['totalURL']);
-								if ($urlParts['host'] == '') {
-									$LD['totalURL'] = 'http://' . $tCR_domain . ($LD['totalURL']{0} == '/' ? '' : '/') . $LD['totalURL'];
+								if ($urlParts['host'] == '' && $currentDomain != $targetDomain) {
+									$LD['totalURL'] = 'http://' . $targetDomain . ($LD['totalURL']{0} == '/' ? '' : '/') . $LD['totalURL'];
 								}
 							}
 							$this->lastTypoLinkUrl = $this->URLqMark($LD['totalURL'],'').$sectionMark;
