April 2024
M T W T F S S
1234567
891011121314
15161718192021
22232425262728
2930  

Categories

April 2024
M T W T F S S
1234567
891011121314
15161718192021
22232425262728
2930  

apache redirect

Create your own short links with the mod_rewrite feature of the Apache HTTP server.

A long time ago, people started sharing links on Twitter. The 140-character limit means that the URL may consume most (or all) of a tweet, so people use URLs to shorten the service. In the end, Twitter added a built-in URL shortening service ( t.co ).

The number of characters is not important now, but there are other reasons to shorten the link. First, shortening the service can provide analytics—you can see the popularity of the links you share. It also simplifies making URLs that are easy to remember. For example, bit.ly/INtravel is easier to remember than https://www.in.gov/ai/appfiles/dhs-countyMap/dhsCountyMap.html . If you want to share a link in advance, but you don’t know the final address, then the URL shortening service can come in handy. .

As with any technology, URL shortening services are not all positive. By blocking the final address, shortened links can be used to point to malicious or offensive content. However, if you go online, the URL shortening service is a useful tool.

We previously posted an article on shortening the service on the website , but maybe you want to run some shortened services supported by simple text files. In this article, we’ll show you how to set up your own URL shortening service using the mod_rewrite feature of the Apache HTTP server. If you are not familiar with the Apache HTTP server, check out David Both ‘s article on installing and configuring it.

Create a VirtualHost
In this tutorial, I assume that you have purchased a cool domain name that you specifically use for the URL shortening service. For example, my website is funnelfiasco.com , so I bought funnelfias.co for my URL shortening service (well, it’s not very short, but it can satisfy my vanity). If you are not running the shortened service as a separate domain, skip to the next section.

The first step is to set up the VirtualHost that will be used for the URL shortening service. For more information on VirtualHost, see the article by David Both . This step only takes a few lines:

<VirtualHost *:80>
ServerName rmohan.com
</VirtualHost>

Create a rewrite rule
This service uses the HTTPD rewrite engine to rewrite the URL. If you created VirtualHost in the section above, the following configuration jumps to your VirtualHost section. Otherwise jump to the server’s VirtualHost or primary HTTPD configuration.

RewriteEngine on
RewriteMap shortlinks txt:/data/web/shortlink/links.txt
RewriteRule^/(.+)$ ${shortlinks:$1}[R=temp,L]
The first line just enables the rewrite engine. The second line builds a short link mapping in a text file. The path above is just an example. You need to use a valid path on your system (make sure it can be read by a user account running HTTPD). The last line rewrites the URL. In this case, it accepts any characters and looks them up in the rewrite map. You may want to use a specific string when rewriting. For example, if you want all shortened links are “slX” (where X is a number), then the above (.+)is replaced (sl\d+).

I am using a temporary redirect (HTTP 302) here. This will allow me to update the target URL later. If you want short links to always point to the same destination, you can use a permanent redirect (HTTP 301). By permanentreplacing the third row temp.

Build your map
Edit the configuration file RewriteMapspecifies the file line. The format is a space-separated key-value store. Put a link on each 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 process. This is done by systemctl restart httpdsimilar command or completed (commands and daemons names may differ due to release). Your link shortening service is now up and running. There is no need to restart the web server when you are ready to edit the map. All you have to do is save the file and the web server will get the difference.

future career
This example gives you a basic URL shortening service. If you want to develop your own management interface as a learning project, it can be a good starting point. Or you can use it to share easy-to-remember links to URLs that are easy to forget.

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>