Jul. 30., 2011

Hiding repeated segments with RealURL

Author: . License: Creative Commons License CC BY-SA 3.0, published on t3node.com

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

--> Back to the list of articles

License

Licensed under creative commonsThis article is licensed under the Creative Commons License CC BY-SA 3.0. You are free to share (copy, distribute and transmit) and to remix (to adapt) the work under the following conditions:

  • You must attribute the work by mentioning the name of the author (Steffen Müller) and setting a link back to the original article using its URL.
  • If you alter, transform, or build upon this work, you may distribute the resulting work only under the same or similar license to this one.

Comments

  1. Tim wrote on July 30, 2011 at 14:40

    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...

  2. Tomaž wrote on July 30, 2011 at 17:32

    Tim, thank you for the tip!

  3. Fabrizio Branca wrote on August 11, 2011 at 10:15

    Hi Tomaž,

    nice solution! Thanks a lot for sharing!

    Bye, and have a nice day,

    Fabrizio :)

  4. Mathias Schopmans wrote on August 25, 2011 at 16:29

    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',
      ),
    ),

  5. Andrea wrote on September 16, 2011 at 11:57

    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

  6. Hauke wrote on November 12, 2011 at 15:48

    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

  7. Klaus wrote on December 2, 2011 at 12:20

    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.

  8. Klaus wrote on December 2, 2011 at 12:38

    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...

  9. Mike wrote on August 29, 2012 at 11:05

    Hi,
    Thanks for this. Very helpful

  10. Patrick wrote on December 11, 2012 at 10:35

    Hey Tomaž,

    thanks for sharing this great code with us all!

    Regards, Patrick

Leave a comment

Sorry, due to technical issues, commenting is disabled at the moment. I'm on it.