Extension:UrlShortener

From MediaWiki.org
Jump to navigation Jump to search
Not to be confused with Extension:ShortUrl.
MediaWiki extensions manual
Crystal Clear action run.svg
UrlShortener
Release status: beta
UrlShortener.png
Implementation Special page, API
Description Implementation of Url Shortener RfC
Author(s) Yuvipandatalk
Latest version 1.0.1 (2014-06-15)
MediaWiki 1.25+
PHP 5.4+
Database changes Yes
License Apache License 2.0
Download
Parameters
  • $wgUrlShortenerDBName
  • $wgUrlShortenerDomainsWhitelist
Hooks used
LoadExtensionSchemaUpdates
WebRequestPathInfoRouter
ResourceLoaderGetConfigVars
Translate the UrlShortener extension if it is available at translatewiki.net
Check usage and version matrix.
Issues Open tasks · Report a bug

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 Help:Extension:UrlShortener.

Installation[edit]

  • If using Vagrant , install with vagrant roles enable urlshortener --provision
Manual installation
  • Download and place the file(s) in a directory called UrlShortener in your extensions/ folder.
  • Add the following code at the bottom of your LocalSettings.php:
    wfLoadExtension( 'UrlShortener' );
    
  • Run the update script which will automatically create the necessary database tables that this extension needs.
  • Yes Done – Navigate to Special:Version on your wiki to verify that the extension is successfully installed.

To make //wiki.ext/r/foo work properly, you will need to set up rewrite rules for your webserver. Example rules for Apache are provided inside the extension.

Configuration[edit]

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 /wiki/Special:UrlRedirector/5234 since it will work regardless of web server configuration.

$wgUrlShortenerTemplate = false;

If you wanted your short URLs in the form of domain.org/r/5234, you would set:

$wgUrlShortenerTemplate = '/r/$1';
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.

$wgUrlShortenerDBName = false;
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).

$wgUrlShortenerDomainsWhitelist = false;

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

$wgUrlShortenerDomainsWhitelist = array(
	'(.*\.)?wikimedia\.org',
	'(.*\.)?wikipedia\.org',
);

If we wanted to whitelist any domain:

$wgUrlShortenerDomainsWhitelist = array( '.*' );

Rewrite rules[edit]

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 .htaccess file comes with the extension. If our template was set up to /r/$1, then we could use:

RewriteEngine On
RewriteRule ^r/(.*)$ /w/index.php?title=Special:UrlRedirector/$1 [PT]

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

location ~ ^/r/(.*) {
	return 301 /w/index.php?title=Special:UrlRedirector/$1;
}

This assumes your $wgScriptPath is set to /w.

API[edit]

This extension provides the API module action=shortenurl to get the shortened url for a given url, creating it if does not exist already.

  • url - URL to shorten.

Response:

{
    "shortenurl": {
        "shorturl": "http://127.0.0.1:8080/s/3"
    }
}

See also[edit]