RealURL
1.2.4. Handling relative links with Speaking URLs
By default, TYPO3 generates all links to other pages as www.server.com/index.php?id=123&type=0, so all pages seem to be in one (filesystem-) directory: the root of the website. The problem is, that many extensions (and TYPO3 core code) rely on images, javascripts, etc. to be in a directory relative to the TYPO3-root, like "typo3/ext/indexed_search/pi/res/pages.gif". This approach doesn't work when the path is constantly changing.
For example, a file "fileadmin/my_image.jpg" referenced from "index.php?id=123" will be found because "index.php" is in the root of the website where also the "fileadmin/" folder is. But as soon as the URL "index.php?id=123" is encoded into a Speaking URL, say "contact/company_address/", then your browser will try to find the image in "contact/company_address/fileadmin/my_image.jpg" where is obviously isn't located.
So to solve this problem you
-
either have to prefix all relative references with an absolute path to the site root
-
or set the <base> tag in the HTML files header to the site root.
config.absRefPrefix
There is a TypoScript-setup directive to set an absolute prefix to all links and images (config.absRefPrefix), but sadly enough that isn't implemented in all places (the indexed-search and front-end-editing for example), so that doesn't work too well.
Please don't use config.absRefPrefix. It has some nasty properties that render RealURLs complete unusable sometimes. The only problem is that the 404-page of TYPO3 doesn't have the <base>-tag, so it doesn't show the TYPO3-logo :)
Support for this might be allowed when the bugs are fixed but generally it will require all code generating reference to use this method and that cannot be guaranteed for all extensions of course.
<base> tag
There is a very simple solution in HTML though: just supply the <base>-tag in the <head> of your pages, like:
<base href="http://your.domain.com/">
To make your TypoScript templates RealURL-enabled, you should therefore include this statement in your HTML-templates, or use the following TypoScript snippet:
config.baseURL = 1
This will automatically read what the current base URL is on your website (using t3lib_div::getIndpEnv('TYPO3_SITE_URL')) and create a <base> tag in the header of the HTML output in the frontend.
If for some reason you want to set another URL instead of the automatically created one can just set it like this:
config.baseURL = http://your.domain.com/
The <base> tag method seems to work flawlessly in TYPO3 except in two cases where you have a link like <a href="#anchor_point"....> - this will not work because it refers to the site root but obviously is an 'internal' reference in the current document.
However you can solve this situation in a simple way:
config.prefixLocalAnchors = all
This will set the needed prefix for all occurencies of '<a href="#...."....' in the page; basically anywhere a local anchor is generated. This substitution happens by a ereg_replace on the general page content after rendering. See TSref for details.
The other situation is specific for MSIE; When you set the "document.location" via JavaScript, MSIE will understand relative URLs as relative to the current URL, not the <base> URL. Therefore you will have to prefix the base URL. You can find that value in $GLOBALS['TSFE']->baseUrl (or use t3lib_div::getIndpEnv("TYPO3_SITE_URL")).
Warning: It might be dangerous to enable "config.baseURL = 1" (the automatic creation of base URL) if you are able to access the website from an IP address or internal network. In such a case the base URL could be "http://192.168.1.123/my_site/" which a) will not work for anyone visiting your site from outside and b) reveal information about the server directory structure (security). This problem will not appear if pages are not cached when visited from the internal network but if pages are cached during internal visits to the site the internal base URLs will be on the pages that people from outside see, hence the problem! Actually the same is true if pages are generated and cached by "index.php?id=123" and then later requested by a speaking URL - then a page generated from a "wrong" location will show the cached content of the wrong location.
Making extensions compatible with "config.baseURL"
If you set "config.baseURL" and subsequently "config.prefixLocalAnchors = all" then extensions might still produce wrong local anchors. That is if extensions are including un-cached page content by USER_INT or USER_EXT cObjects that content is not processed! (unless "config.prefixLocalAnchors" is set to "output"). For such extensions there should be inherent support for "realurl" and that can be done (with full backwards compatibility) by prefixing all local anchors made by the result of this:
substr(t3lib_div::getIndpEnv('TYPO3_REQUEST_URL'),strlen(t3lib_div::getIndpEnv('TYPO3_SITE_URL')));
or in recent TYPO3 versions:
$GLOBALS['TSFE']->anchorPrefix
Generally
Make sure you include either configuration it in ALL page-types that are generated!