Run Your Own URL Shortener on Raspberry Pi

Long URLs are difficult to remember. With a self-hosted URL shortener, you need only remember your own domain name and a short string. Here’s how to set up and use one on your Raspberry Pi.

Why Do You Need a URL Shortener?

URLs are long unwieldy things, and can be difficult to remember. The URL for this post, for instance, ishttps://www.makeuseof.com/run-your-own-url-shortener-on-raspberry-pi/. That’s 69 characters, and if you consider it worth sharing as part of a social media post with a set character limit, that’s 69 characters you could be using to wax lyrical with commentary, and your own take on the article. Shorter URLs give you more space to write.

A URL shortener takes a long URL, assigns it a new, shorter URL, and redirects traffic to the original long URL. As an example, clicking onhttps://tinyurl.com/2p8xswauwill send a request to TinyURL, which will redirect the request tohttps://www.makeuseof.com/author/david-rutland/, this author’s profile page. By using a URL shortener, you’ve saved a huge 41 characters.

4

They’re also super-handy if you plan on telling someone a web address—or even writing it down—rather than sending a link.

Otherexcellent reasons for using a URL shortenerinclude branding and marketing, tracking clicks, and deep linking to apps.

raspberry pi 3 model b plus with black background

Why Host a URL Shortener on Raspberry Pi?

There are many commercial URL shortening services available. Some are entirely paid for, others are completely free to use, while a fair proportion operate on a ‘Freemium’ model, offering basic functionality without cost, while charging for more than basic usage.

You may have already heard of, or even used URL shortening services such as TinyUR, Bitly, or BL.INK. These services host your collections of shortened URLs, but they’re vulnerable and not especially portable. If you break the terms of service, your collection could vanish in an eyeblink. If the service provider increases charges to a level you’re not comfortable with, you can either accept the extortionate price rise or lose your URL collection.

namecheap dns records for muo.lol

And then there’s the fact that URLs likehttps://tinyurl.com/2p8xswau, while certainly shorter than the alternative, are neither particularly easy to remember, nor especially short.

By running your on URL shortener on a Raspberry Pi, you’re not vulnerable to price rises, the vagaries of terms and conditions, and you can customize the URL however you like. We’ve chosenPolras our URL shortener as it’s easy to install, simple to use, and comes with great support.

polr config screen with a mountainous background

Choose a Short Domain Name

The key feature of a URL shortener is that the resulting URL should be, well, short, and to achieve that, you need a short domain name. All of the good .com domains are gone, but there are hundreds of other top-level domains you can use as your base.

We chosemuo.lol. It’s a mere seven characters, easy to remember, and is related to the main website. We could have chosen an equally effective muo.fun, muo.gay, muo.la, or muo.wtf. Thepossibilities for a good domain nameare endless.

muo link shortener at muo.lol

Register the domain (ours cost $1.98 for year), then visit the advanced DNS section. Create a new record, choosingAas the type,@as the host, and for the value, put your own home IP address. Set the TTL as low as possible.

Set Up Your Raspberry Pi With Polr

The first thing you need to do is follow our guide on how to set up your Raspberry Pi as a server. Once you’ve done that, log in with Secure Shell (SSH), then update and upgrade any installed packages:

Change directory, clone the Polr GitHub repository, and set permissions:

Now give ownership of thepolrdirectory to the Apache user:

Move into thepolrdirectory, then make yourself the root user, and install the Composer tool and dependencies:

You will see a warning: “Do not run Composer as root/super user!” However, it’s safe to ignore this. After this point, Composer may throw errors relating to missing PHP modules; install them with:

And then repeat the previous command. When asked, “Do you trust ‘kylekatarnls/update-helper’ to execute code and wish to enable it now?” hitYon your keyboard and thenReturn. You will be returned to the command prompt. Exit the root shell, then change directories:

Enable the Apache rewrite mod:

And use nano to create a new Apache conf file:

In it, paste:

…Replacingmuo.lolwith your own domain name. Save and exit nano withCtrl + OthenCtrl + X.Enable the new conf file, then restart Apache with:

Polr requires a database to run. Open MariaDB with:

Add the following entries, one line at a time:

Move back to the Polr setup directory and copy the.envfile:

you’re able to now run the configuration wizard through your browser athttp://your-domain/setup/

Set Up Polr Through a Browser

You’ll be faced with a bewildering array of options and text input fields against a beautiful polar background. Here you’ll tell your Raspberry Pi URL shortener what it is, and how you want it to work. Some of these fields will be pre-filled.

Leave theDatabase HostandDatabase Portwith their default values, but change theDatabase Usernametopolrand the password to whatever you set in MariaDB. If you followed our instructions exactly, this will betopsecretpassword. The Application URL is your domain name.

The rest of the options relate to how you want your short URLs formatted, whether you need a password in order to use the site, and if you want your URLs public. Remember to set a sensible admin username and secure password. You can also select from a variety of themes including Google Material Design, Newspaper, and Midnight Black. When you’re done, hit the greenInstallbutton at the bottom of the page.

Secure Your URL Shortener

Man-in-the-middle attacksare no fun for anyone (except, perhaps, for the man himself). Secure your Polr installation by using Certbot to fetch and install certificates and keys from Let’s Encrypt.

Enter your email address when requested, and select which name would you like to activate HTTPS for. Certbot will fetch and install SSL keys and certificates from Let’s Encrypt. Now restart Apache:

When you reload the page in your browser, the connection to your URL shortener will be secure. To shorten a URL, simply paste the URL into the text box, and clickShorten. Or clickLink optionsto customize the short link.

Use Your Raspberry Pi URL Shortener With Care!

Running your own URL shortener is a great way to keep track of links you find useful and those you would like to share with others. Since your URL shortener is on the public internet, however, be careful that you do not use it to create links to illegal, ethically questionable, or pirated material.

A URL shortener is just one of the many internet-related projects you can run on a Raspberry Pi. Others include the ability to host your photo gallery, password manager, and even a personal cloud storage server with a variety of useful apps.

When you host Nexcloud on your Raspberry Pi, not only do you get a private cloud storage server but a bunch of excellent apps for all purposes.

It’s not super flashy, but it can help to keep your computer up and running.

Turn these settings on, and your iPhone will be so much better than before.

One casual AI chat exposed how vulnerable I was.

Don’t let aging hardware force you into buying expensive upgrades.

Who asked for these upgrades?

Technology Explained

PC & Mobile