URL shortener / TinyURLs 

Version

main

Language

en

Author

Alexander Stehlik

License

This extension documentation is published under the CC BY-NC-SA 4.0 (Creative Commons) license

This extensions allows you to cut down long URLs. It basically works like bitly or TinyURL.

Introduction 

What does it do? 

It converts a normal URL to a tiny URL. It works similar as services provided by https://bitly.com/ or http://tinyurl.com/ .

It has multiple integration options. You can create tiny URLs in the backend by adding a tiny URL database record. You can convert any typolink to a tinyurl by simply adding some configuration options. And you can use the API of this extension to create tiny URLs in your own extensions.

Screenshots 

Settings for a tiny URL in the Backend;

Screenshot of the Backend form for editing a tinyurl record.

Usage 

You can either use the extension as an administrator / integrator by using additional TypoScript (see Administrator Manual section) or as a developer by using the API of the extension (see Developer Corner section).

In-depth information 

About Tiny URL Keys 

A tiny URL key consists of two parts:

  1. a base62 encoded integer based on a custom dictionary
  2. and an optional random part, seperated by a dash

A URL key might look like this:

Aefc-3E
Copied!

The first part is generated from the UID of the tiny URL database record which is converted to a base62 integer.

The second part (after the dash) is simply a random hexadecimal string.

In the extension configuration you can edit two options that influence the tiny URL key generation. If minimalTinyurlKeyLength is set the URL key must have at least this amount of characters. If the base62 part is shorter the missing characters will be appended. An example:

  • minimalTinyurlKeyLength is 6
  • the generated base62 part is Aefc
  • the URL generator will add two more characters after a dash
  • if the minimalTinyurlKeyLength was only 4 no more characters would have been added

Another option is the minimalRandomKeyLength . If this is set to a value greater than zero the defined number of random characters will always be appended to the base62 part. Another example:

  • minimalTinyurlKeyLength is 4
  • minimalRandomKeyLength is 3
  • the generated base62 part is Aefc
  • the URL generator will add three more characters after a dash

Administrator Manual 

Installation 

The first step, to get this extension up and running is importing it in the extension manager and install it.

Alternatively you can use composer to install the extension:

composer require de-swebhosting-typo3-extension/tinyurls
Copied!

Then you can edit the extension configuration according to your needs.

Now you are able to create and use tiny URLs in your TYPO3 instance.

Editing tiny URL records 

Once the extension is installed you can add tiny URLs in the TYPO3 backend. If you didn't change the default value of urlRecordStoragePID in the extension configuration you must add the records in the TYPO3 root. Otherwise you can add them in the page (folder) with the PID you specified in the config.

Using tinyurls in TypoScript 

You can convert any typolink to a tinyurl by simply setting the property tinyurl to 1. For more information about the TypoScript configuration options please have a look in the TypoScript configuration section.

This is a quick example:

page.30 = TEXT
page.30 {
		value = Permalink
		typolink = 1
		typolink.parameter.data = getIndpEnv:REQUEST_URI
		typolink.tinyurl = 1
}
Copied!

Further information 

Speaking URL configuration 

By default, a tiny URL created by this extension will look like this:

http://mytypo3page.tld/index.php?eID=tx_tinyurls&tx_tinyurls[key ]=Aefc-3E

This is not very short and readable. This is why using speaking URLs is recommended. For this to work you need to set createSpeakingURLs to 1 and maybe edit the speakingUrlTemplate . By default, speaking URLs will look like this:

http://mytypo3page.tld/goto/Aefc-3E

Obviously you will need to tell you webserver how to handle these URLs.

Apache example configuration 

When you are using mod_rewrite you can use this line in your .htaccess file or your webserver configuration:

RewriteRule ^tinyurl/([a-zA-Z0-9]+(-[a-zA-Z0-9]+)?)$ /index.php?eID=tx_tinyurls&tx_tinyurls[key]=$1

Copied!

Lighttpd example configuration 

When you use lighttpd you can use this configuration for rewriting tiny URLs:

url.rewrite-if-not-file = (
    # rewrite goto urls to tinyurls extension
    "^/tinyurl/(.*)$" => typo3path + "index.php?eID=tx_tinyurls&tx_tinyurls[key]=$1",
)
Copied!

Configuration Reference 

Most of the configuration is done in the extension configuration.

New in version 13.x: Every Extension configuration setting can now be overwritten in the Site Configuration in the tinyurls key, e.g.:

tinyurls:
  baseUrl: 'https://example.com'
Copied!

When using the typolink configuration, there are also additional TypoScript settings available.

Extension configuration 

Property
baseUrl
Data type
string
Description
This URL will be used as prefix for non speaking URLs and will also override the TYPO3_SITE_URL placeholder in the speakingUrlTemplate.
Default
<Empty string>
Property
baseUrlFromSiteBase
Data type
boolean
Description
If this enabled the prefix for non speaking URLs and the value of the TYPO3_SITE_URL placeholder in speaking URLs will be taken from "base" setting of the active site configuration.
Default
0
Property
createSpeakingURLs
Data type
boolean
Description
If true (1) then the tiny URL will be generated depending on the speakingUrlTemplate. Hint! If you enable this you might need to add a rewrite rule to you webserver!
Default
0
Property
speakingUrlTemplate
Data type
string
Description
The template that is used for creating a speaking URL (only relevant if createSpeakingURLs is set to 1). You can use all available keys for TYPO3CMSCoreUtilityGeneraulUtility::getIndpEnv() as template markers (e.g. ###TYPO3_SITE_URL### and the ###TINY_URL_KEY### template marker will be replaced with the shortened URL key.
Default
###TYPO3_SITE_URL###tinyurl/###TINY_URL_KEY###
Property
base62Dictionary
Data type
string
Description
Dictionary for creating bas 62 based integers (see http://jeremygibbs.com/2012/01/16/how-to-make-a-url-shortener), use random string to increase security (e.g. http://textmechanic.com /String-Randomizer.html)
Default
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789
Property
minimalRandomKeyLength
Data type
integer
Description
The minimum length that the random part of the tiny URL must have.
Default
2
Property
minimalTinyurlKeyLength
Data type
integer
Description
The minimum length that the whole tiny URL key must have.
Default
8
Property
urlRecordStoragePID
Data type
integer
Description
The PID where the tiny URL records are stored, use 0 to store them in the TYPO3 root.
Default
8

TypoScript Reference 

These additional properties are available for typolink objects when the extension is installed:

Property
tinyurl
Data type
boolean
Description

If you set this to 1 (TRUE) the final typolink URL is converted to a tiny URL.

You an set some configuration options for the tiny URL generator int the tinyurl. namespace (see below).

Default
0
Property
tinyurl.deleteOnUse
Data type
boolean /stdWrap
Description
If this is is true, the tiny URL is deleted from the database on the first hit. This makes the most sense when used together with the urlKey property.
Default
0
Property
tinyurl.validUntil
Data type
integer /stdWrap
Description
The timestamp until the URL is valid. If this is set to 0 the URL will never be invalid.
Default
0
Property
tinyurl.urlKey
Data type
string /stdWrap
Description
Normally the URL key is generated automatically. Here you can set you own unique urlKey. This may be used together with the deleteOnUse property for creating one-time valid URLs, e.g. for activating a subscription.
Default
0

Developer Corner 

Using tinyurls in your extension 

When you want to generate tiny URLs in your own extension you can use the \Tx\Tinyurls\Domain\Model\TinyUrl class to set the properties and then use the \Tx\Tinyurls\TinyUrl\TinyUrlGeneratorInterface service to generate the Tiny URL.

Please have a look at the PHPDoc annotations for further information. This is a quick example how to use it:

<?php

   use Tx\Tinyurls\Domain\Model\TinyUrl;
   use Tx\Tinyurls\TinyUrl\TinyUrlGeneratorInterface;

   class MyDemo
   {
       public function __construct(private TinyUrlGeneratorInterface $tinyUrlGenerator)
       {
       }

       public function createTinyUrl(string $theKey, DateTimeInterface $validUntil): string
       {
           $tinyUrl = TinyUrl::createForUrl('http://www.typo3.org');
           $tinyUrl->enableDeleteOnUse();
           $tinyUrl->setCustomUrlKey($theKey);
           $tinyUrl->setValidUntil($validUntil);

           return $this->tinyUrlGenerator->generateTinyUrl($tinyUrl);
       }
   }
Copied!

Sitemap