Hiding repeated segments with RealURL
This article demonstrates how to remove parts of an URL in TYPO3 using RealURL. Especially the excessive use of getParams of Extbase plugins results in superfluous repeated parts in URLs. Guest author Tomaž Zaman will show you how to tackle that problem.
By now, everyone has probably heard of building extensions the 'new' way, with extbase and fluid. Numerous extensions build with those are poping up daily in the TER.
The only minor drawback (as I see it) is their URL generation, because URLs tend to fill up pretty much the whole width of the address bar in your browser (context, controller, action, cHash), which looks quite ugly - probably not the best solution from SEO perspective either.
RealURL can be a great solution to this problem, but needs a bit of customizing, to get the optimal results. The extension I'll use to demonstrate this solution is called 'YAG' (Yet another gallery) by Daniel Lienert and Michael Knoll (thanx, guys!). It's worth noting, that the extension already includes some RealURL configuration functionality, but still lacks the final touch.
I've added the following snippet to my realurl_conf.php:
'postVarSets' => array(
'_DEFAULT' => array(
'album' => array(
array(
'GETvar' => 'tx_yag_pi1[controller]',
),
array(
'GETvar' => 'tx_yag_pi1[action]',
),
array(
'GETvar' => 'tx_yag_pi1[context43][albumUid]',
'lookUpTable' => array(
'table' => 'tx_yag_domain_model_album',
'id_field' => 'uid',
'alias_field' => 'name',
'addWhereClause' => ' AND deleted !=1 AND hidden !=1',
'useUniqueCache' => 1,
'useUniqueCache_conf' => array(
'strtolower' => 1,
'spaceCharacter' => '-',
)
)
),
array(
'GETvar' => 'cHash',
),
)
),
),
This produced the following result (on the single album view):
http ://example.com/album/ItemList/submitFilter/my-album-title/cHash/
Much better since the original output, but still had two unnecessary segments: ItemList and submitFilter. Not only are those not needed in my case (they were always the same), they don't 'fit' in my URLs where pretty much all other segments are always in Slovene.
After going through the code of the provided hooks (by YAG), I decided to write my own, which turned out to be much easier and faster way than I first thought it would be. First, the hooks (also in realurl_conf.php):
function user_encodeSpURL_postProc(&$params, &$ref) {
$params['URL'] = str_replace('album/ItemList/submitFilter/', 'album/', $params['URL']);
}
function user_decodeSpURL_preProc(&$params, &$ref) {
$params['URL'] = str_replace('album/', 'album/ItemList/submitFilter/', $params['URL']);
No rocket science here, I simply replaced the longer version of the URL with shortened one. I kept the word album in both functions, so I know what function does and I don't mistakenly rewrite some other URLs.
(In case you don't know, two functions are used because one is used for generating the URLs and the second one 'translates' the URL back to GET vars for TYPO3)
All we're left now is calling those two hooks from inside the RealURL configuration:
$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['realurl'] = array(
'encodeSpURL_postProc' => array('user_encodeSpURL_postProc'),
'decodeSpURL_preProc' => array('user_decodeSpURL_preProc'),
'_DEFAULT' => array(
// the rest of your configuration
And voila! My URLs look like this now:
http ://example.com/album/my-album-title/cHash/
which I like much better and I think google crawler will agree with me.
About the author
Tomaž
Zaman is a Slovenian freelancer with more than 7 years of experience with TYPO3. He has made numerous
websites, from small 4-page portfolios, to educational, government and corporate websites and intranets for
clients from all across Europe. Since 2011 he is a certified TYPO3 integrator.
OPTISS
@:
tomaz@optiss.si
W:
http://optiss.si
T: +386
30 651 645
Comments
Nice Idea... to improve the code. Don't use cHash as postVarSets and enable the RealURL cHash cache. Then the cHash will disapear if there are no "normal" get vars...
Tim, thank you for the tip!
Hi Tomaž,
nice solution! Thanks a lot for sharing!
Bye, and have a nice day,
Fabrizio :)
It's much simpler to put the unwanted vars in preVars and set noMatch to bypass.
Example: hide tt_products[backPID] and tt_products[cat]
'preVars' => array (
array (
'GETvar' => 'L',
'valueMap' => array (
'en' => '0',
'de' => '1',
'fr' => '2',
),
'noMatch' => 'bypass',
),
array(
'GETvar' => 'tt_products[backPID]',
'noMatch' => 'bypass',
),
array(
'GETvar' => 'tt_products[cat]',
'noMatch' => 'bypass',
),
),
Hi,
thanks for this,
but i still have problems with realurl for tt_products.
The parameter in detailview is allways like below, but I like to have the product title instead. Has anyone a solution.
This is shown:
example.com/details/?tt_products[product]=1367&cHash=e6b7c6f71db72fd632d229d1e19c8fdf
I want something like this:
example.com/details/fantasticbike.html
Best,
Andrea
There is a new functionality in 1.4 to skip default arguments if the argument is the default:
http://forge.typo3.org/projects/typo3v4-mvc/wiki/Skip_default_arguments_in_URIs
I agree with Mathias Schopmans. I tried to use the functions, but I wouldnt work. Using the "preVars" and setting "bypass" is a lot easier.
Sorry. My last post was bullsh**. If you use the bypass and the preVars, then YAG does not work anymore. I should have tried it before writing a post...
Hi,
Thanks for this. Very helpful
Hey Tomaž,
thanks for sharing this great code with us all!
Regards, Patrick
You do not need php for that. This solution is too heavy and dirty.
You just need empty postVars for action/controller with noMatch=bypass and a TS condition that sets config.defaultGetVars to action and controller on that page.