MediaWiki Template Get Hostname

I was working on a template for one my personal wikis and needed to get the hostname for a given URL. Using the capabilities of the Parser Functions extension for MediaWiki I whipped up this template. I figured others may find this useful so here it is. The first version has a bunch of spaces and newlines added to make it more readable.

{{#vardefine: hoststart | {{#expr: {{#pos: {{{1|}}} | // }} + 2 }} }}
{{#vardefine: hostend | {{#pos: {{{1|}}} | / | {{#expr: {{#pos: {{{1|}}} | // }} + 2 }} }} }}
{{#vardefine: hostlen | {{#expr: {{#var: hostend }} - {{#var: hoststart }} }} }}
{{#sub: {{{1|}}} | {{#var: hoststart}} | {{#var: hostlen}} }}

To put it in your own MediaWiki, copy this version that removes the spaces and newlines.

To use this template put it on a page like Template:Get hostname and then call it in your pages as

{{Get hostname|http://thingelstad.com/another-reason-you-need-to-use-a-password-manager/}}

which will return thingelstad.com. You can also find this template on MediaWiki Cookbook.

3 thoughts on “MediaWiki Template Get Hostname

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>