Extension:UrlShortener

The UrlShortener extension provides a basic URL shortening service as a MediaWiki extension. It will require some changes to your web server's configuration to operate properly. It was originally designed to implement the URL Shortener RfC.

For user documentation, see Wikimedia URL Shortener.

LocalSettings.php

 * URL routing configuration

Configures the template to use when generating the shortened URL. Using this feature will require mod_rewrite (or an equivalent). If set to false (default), the short URLs will use the not-so-short  since it will work regardless of web server configuration.

If you wanted your short URLs in the form of, you would set:


 * Short domain name

If you have a custom short domain name, you can set it by using:

If set to false (default), it will use $wgServer.


 * Global database

Set this to the name of a database if you wish to use one central database for your wiki farm. If set to false (default), it will use the wiki's normal database.

If the database is on an external cluster, you will also need to configure that.


 * Allow arbitrary ports

By default, only URLs with ports 80 and 443 are accepted and are automatically removed. If your wiki is set up using a custom port, set this to true to allow shortening URLs that have arbitrary ports.


 * Whitelist regex

Configures the acceptable domains that users can submit links for. This is an array of regular expressions. If set to false (default), it will set up a whitelist for the current domain (using $wgServer).

For example, to only whitelist links to wikipedia.org or wikimedia.org, we would use the following:

If we wanted to whitelist any domain:


 * Whitelist documentation

To provide human-readable documentation of the whitelist, this is an array of the whitelisted domains that will be displayed on Special:UrlShortener.

If set to false (default), it will output a normalized version of $wgServer.

If you only whitelisted wikipedia.org and wikimedia.org in the above example:


 * Shortcode character set

If you want to customize the character set the shortcodes use, you can override this setting. If changed, any existing short URLs will go to the wrong destination.

The first character in the list is treated as a leading zero; no shortcodes beginning with that character will be created, and it is ignored when used at the start of the shortcode in a URL (e.g. https://w.wiki/22222222w is the same as https://w.wiki/w).


 * Read-only mode

Set $wgUrlShortenerReadOnly to true to prevent users from creating new short URLs. This is mainly intended as a hack while deploying to Wikimedia sites and will be removed once it is no longer needed.

VirtualHost or .htaccess
To have truly short URLs, you will need to set up rewrite rules, using mod_rewrite or something similar. An example of what to put in your  file comes with the extension. If our template was set up to, then we could use:

If you are using nginx, you can add the following within a  definition.

This assumes your is set to.

API
This extension provides the API module  to get the shortened url for a given url, creating it if does not exist already. See the API documentation here: https://meta.wikimedia.org/w/api.php?action=help&modules=shortenurl.


 * - URL to shorten.

Response: