Science and technology

How to construct a URL shortener with Apache

Long in the past, people began sharing hyperlinks on Twitter. The 140-character restrict meant that URLs may devour most (or all) of a tweet, so folks turned to URL shorteners. Eventually, Twitter added a built-in URL shortener (t.co).

Character rely is not as essential now, however there are nonetheless different causes to shorten hyperlinks. For one, the shortening service might present analytics—you possibly can see how common the hyperlinks are that you simply share. It additionally simplifies making easy-to-remember URLs. For instance, bit.ly/INtravel is way simpler to recollect than https://www.in.gov/ai/appfiles/dhs-countyMap/dhsCountyMap.html. And URL shorteners can come in useful if you wish to pre-share a hyperlink however do not know the ultimate vacation spot but.

Like any know-how, URL shorteners aren’t all optimistic. By masking the final word vacation spot, shortened hyperlinks can be utilized to direct folks to malicious or offensive content material. But in case you surf rigorously, URL shorteners are a great tool.

We covered shorteners previously on this website, however perhaps you need to run one thing easy that is powered by a textual content file. In this text, we’ll present the best way to use the Apache HTTP server’s mod_rewrite function to arrange your personal URL shortener. If you are not acquainted with the Apache HTTP server, take a look at David Both’s article on installing and configuring it.

Create a VirtualHost

In this tutorial, I am assuming you purchased a cool area that you will use solely for the URL shortener. For instance, my web site is funnelfiasco.com, so I purchased funnelfias.co to make use of for my URL shortener (okay, it isn’t precisely brief, nevertheless it feeds my vainness). If you will not run the shortener as a separate area, skip to the following part.

The first step is to arrange the VirtualHost that might be used for the URL shortener. For extra data on VirtualHosts, see David Both’s article. This setup requires only a few fundamental traces:

    <VirtualHost *:80>
        ServerName funnelfias.co
    </VirtualHost>

Create the rewrites

This service makes use of HTTPD’s rewrite engine to rewrite the URLs. If you created a VirtualHost within the part above, the configuration under goes into your VirtualHost part. Otherwise, it goes within the VirtualHost or fundamental HTTPD configuration on your server.

    RewriteEngine on
    RewriteMap shortlinks txt:/information/internet/shortlink/hyperlinks.txt
    RewriteRule ^/(.+)$ $shortlinks:$1 [R=temp,L]

The first line merely permits the rewrite engine. The second line builds a map of the brief hyperlinks from a textual content file. The path above is barely an instance; you will want to make use of a sound path in your system (be certain it is readable by the person account that runs HTTPD). The final line rewrites the URL. In this instance, it takes any characters and appears them up within the rewrite map. You might need to have your rewrites use a selected string originally. For instance, in case you wished all of your shortened hyperlinks to be of the shape “slX” (the place X is a quantity), you’d change (.+) above with (sld+).

I used a brief (HTTP 302) redirect right here. This permits me to replace the vacation spot URL later. If you need the brief hyperlink to all the time level to the identical goal, you should utilize a everlasting (HTTP 301) redirect as an alternative. Replace temp on line three with everlasting.

Build your map

Edit the file you specified on the RewriteMap line of the configuration. The format is a space-separated key-value retailer. Put one hyperlink on every line:

    osdc https://opensource.com/users/bcotton
    twitter https://twitter.com/funnelfiasco
    swody1 https://www.spc.noaa.gov/products/outlook/day1otlk.html

Restart HTTPD

The final step is to restart the HTTPD course of. This is finished with systemctl restart httpd or comparable (the command and daemon identify might differ by distribution). Your hyperlink shortener is now up and operating. When you are able to edit your map, you need not restart the online server. All you must do is save the file, and the online server will choose up the variations.

Future work

This instance provides you a fundamental URL shortener. It can function an excellent start line if you wish to develop your personal administration interface as a studying challenge. Or you possibly can simply use it to share memorable hyperlinks to forgettable URLs.

Most Popular

breakingExpress.com features the latest multimedia technologies, from live video streaming to audio packages to searchable archives of news features and background information. The site is updated continuously throughout the day.

Copyright © 2017 Breaking Express, Green Media Corporation

To Top