Tom
Tom

Reputation: 689

TYPO3 additional url rewrite for a plugin

RealUrl is a great thing, but it doesnt solve all url problems. Many times I need to shorten the plugin's url. Or I have specific situation for shortening the urls...

I'm searching a solution to rewrite the url and let the RealUrl to handle it after. This way I could always additionally append all kinds of rewritings on clients request.

For example I would like to shorten: http://yyy.com/zimmer-details/rooms/doppelzimmer/ to http://yyy.com/room-test/doppelzimmer/

(where: zimmer-details is a document, rooms is an extension and doppelzimmer is a title from a object)

In this case I would like to use:

RewriteRule ^(.*)roomtest(.*)$ $1/zimmer-details/rooms$2 [NC,L]

But sadly, this doesn't work...

If I add R=301 (so the browser will do the redirection) it will work:

RewriteRule ^(.*)roomtest(.*)$ $1/zimmer-details/rooms$2 [NC,L,R=301]

And I would really like to do the redirection internaly..

I'm not that good at .htaccess, so probably I'm missing something. It could be also possible that the TYPO3 is making me problems.

Any solution, tips, leds on this one?

This is the full .htaccess:

### Begin: Settings for mod_rewrite ###

# You need rewriting, if you use a URL-Rewriting extension (RealURL, CoolUri, SimulateStatic).

<IfModule mod_rewrite.c>

    # Enable URL rewriting
    RewriteEngine On

    # Change this path, if your TYPO3 installation is located in a subdirectory of the website root.
    RewriteBase /

    # activate minification of css and js
    RewriteRule ^(.*\.(css|js).*)$ min/index.php?f=$1&debug=0 [L,NC]

    # !!! MY REWRITE !!!
    RewriteRule ^(.*)roomtest(.*)$ $1/zimmer-details/rooms$2 [NC]

    # Rule for versioned static files, configured through:
    # - $TYPO3_CONF_VARS['BE']['versionNumberInFilename']
    # - $TYPO3_CONF_VARS['FE']['versionNumberInFilename']
    # IMPORTANT: This rule has to be the very first RewriteCond in order to work!
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.+)\.(\d+)\.(php|js|css|png|jpg|gif|gzip)$ $1.$3 [L]

    # Stop rewrite processing, if we are in the typo3/ directory.
    # For httpd.conf, use this line instead of the next one:
    # RewriteRule ^/TYPO3root/(typo3/|t3lib/|fileadmin/|typo3conf/|typo3temp/|uploads/|favicon\.ico) - [L]
    RewriteRule ^(typo3/|t3lib/|fileadmin/|typo3conf/|typo3temp/|uploads/|favicon\.ico) - [L]

    # Redirect http://example.com/typo3 to http://example.com/typo3/index_re.php and stop the rewrite processing.
    # For httpd.conf, use this line instead of the next one:
    # RewriteRule ^/TYPO3root/typo3$ /TYPO3root/typo3/index.php [L]
    RewriteRule ^typo3$ typo3/index_re.php [L]

    # Don't pull *.xml, *.css etc. from the cache
    RewriteCond %{REQUEST_FILENAME} !^.*\.xml$
    RewriteCond %{REQUEST_FILENAME} !^.*\.css$

    # Check for Ctrl Shift reload
    RewriteCond %{HTTP:Pragma} !no-cache
    RewriteCond %{HTTP:Cache-Control} !no-cache

    # NO backend user is logged in. Please note that the be_typo_user expires at the
    # end of the browser session. So, although you have already logged out of the
    # backend, you will still have to either restart your browser or remove the
    # cookie manually for this rule to work.
    RewriteCond %{HTTP_COOKIE} !be_typo_user [NC]

    # NO frontend user is logged in. Logged in frontend users may see different
    # information than anonymous users. But the anonymous version is cached. So
    # don't show the anonymous version to logged in frontend users.
    RewriteCond %{HTTP_COOKIE} !nc_staticfilecache [NC]

    # We only redirect GET requests
    RewriteCond %{REQUEST_METHOD} GET

    # We only redirect URI's without query strings
    RewriteCond %{QUERY_STRING} ^$

</IfModule>

### End: Settings for mod_rewrite ###

+ some other caching things ...

Upvotes: 0

Views: 1296

Answers (3)

Tom
Tom

Reputation: 689

Thank to the Fixus I was on a right track again... Forget about the .htaccess! RealUrl can do it all!

Answer to my question is to use RealUrl hooks:

For my example:

function user_encodeSpURL_postProc(&$params, &$ref) {
    $params['URL'] = str_replace('zimmer-details/rooms/', 'room/', $params['URL']);
}

function user_decodeSpURL_preProc(&$params, &$ref) {
    $params['URL'] = str_replace('room/', 'zimmer-details/rooms/', $params['URL']);
}

And include the hooks in the RealUrl:

$TYPO3_CONF_VARS['EXTCONF']['realurl'] = array(
    'encodeSpURL_postProc' => array('user_encodeSpURL_postProc'),
    'decodeSpURL_preProc' => array('user_decodeSpURL_preProc')
);

And the magic happend! I guess with this simple replace hook I can do all kind of wonderful things. Also, you could combine them with the RegEx if needed...

Upvotes: 1

biesior
biesior

Reputation: 55798

For avoiding the current page in speaking path, RealUrl has a configuration set called fixedPostVars, it's used tha same way as common postVars except that instead of the param name you should use the uid of the page on which plugin is placed, like:

35 => array(
  array(
    'GETvar' => 'tx_myext_pi1[no_comments]',
    'valueMap' => array(
      'no-comments' => 1
    ),
    'noMatch' => 'bypass',
  )
),

See great tutorial wroten by RU author - Dmitry Dulepov (fixedPostVars are described in first part):

Keep in mind, that manipulating the RU urls manually within the userfunc can lead (occasionally) to invalid URL handling.

Upvotes: 2

Fixus
Fixus

Reputation: 4641

IMO you don't need to do that in .htaccess

For example I would like to shorten: http://yyy.com/zimmer-details/rooms/doppelzimmer/ to http://yyy.com/room-test/doppelzimmer/

(where: zimmer-details is a document, rooms is an extension and doppelzimmer is a title from a object)

If zimmer-details is a page in BE (this is how I understand document) you can ignore it in URL (RealURL adds checkbox in page settings for that)

rooms can be ignore in realurl_conf.php, if I remember you need to set is as bypass.

When you will set both things your URL will be as short as you want

Upvotes: 1

Related Questions