如何使用 Apache 构建 URL 缩短服务
短链接 Apache

> 
> 用 Apache HTTP 服务器的 mod\_rewrite 功能创建你自己的短链接。
> 
> 
> 


![](/data/attachment/album/201809/19/223103exq5ziz5baxbuix1.png)


很久以前，人们开始在 Twitter 上分享链接。140 个字符的限制意味着 URL 可能消耗一条推文的大部分（或全部），因此人们使用 URL 缩短服务。最终，Twitter 加入了一个内置的 URL 缩短服务（[t.co](http://t.co)）。


字符数现在不重要了，但还有其他原因要缩短链接。首先，缩短服务可以提供分析功能 —— 你可以看到你分享的链接的受欢迎程度。它还简化了制作易于记忆的 URL。例如，[bit.ly/INtravel](http://bit.ly/INtravel) 比<https://www.in.gov/ai/appfiles/dhs-countyMap/dhsCountyMap.html>更容易记住。如果你想预先共享一个链接，但还不知道最终地址，这时 URL 缩短服务可以派上用场。。


与任何技术一样，URL 缩短服务并非都是正面的。通过屏蔽最终地址，缩短的链接可用于指向恶意或冒犯性内容。但是，如果你仔细上网，URL 缩短服务是一个有用的工具。


我们之前在网站上[发布过缩短服务的文章](https://opensource.com/article/17/3/url-link-shortener)，但也许你想要运行一些由简单的文本文件支持的缩短服务。在本文中，我们将展示如何使用 Apache HTTP 服务器的 mod\_rewrite 功能来设置自己的 URL 缩短服务。如果你不熟悉 Apache HTTP 服务器，请查看 David Both 关于[安装和配置](https://opensource.com/article/18/2/how-configure-apache-web-server)它的文章。


### 创建一个 VirtualHost


在本教程中，我假设你购买了一个很酷的域名，你将它专门用于 URL 缩短服务。例如，我的网站是 [funnelfiasco.com](http://funnelfiasco.com)，所以我买了 [funnelfias.co](http://funnelfias.co) 用于我的 URL 缩短服务（好吧，它不是很短，但它可以满足我的虚荣心）。如果你不将缩短服务作为单独的域运行，请跳到下一部分。


第一步是设置将用于 URL 缩短服务的 VirtualHost。有关 VirtualHost 的更多信息，请参阅 [David Both 的文章](https://opensource.com/article/18/3/configuring-multiple-web-sites-apache)。这步只需要几行：



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

### 创建重写规则


此服务使用 HTTPD 的重写引擎来重写 URL。如果你在上面的部分中创建了 VirtualHost，则下面的配置跳到你的 VirtualHost 部分。否则跳到服务器的 VirtualHost 或主 HTTPD 配置。



```
    RewriteEngine on
    RewriteMap shortlinks txt:/data/web/shortlink/links.txt
    RewriteRule ^/(.+)$ ${shortlinks:$1} [R=temp,L]
```

第一行只是启用重写引擎。第二行在文本文件构建短链接的映射。上面的路径只是一个例子。你需要使用系统上使用有效路径（确保它可由运行 HTTPD 的用户帐户读取）。最后一行重写 URL。在此例中，它接受任何字符并在重写映射中查找它们。你可能希望重写时使用特定的字符串。例如，如果你希望所有缩短的链接都是 “slX”（其中 X 是数字），则将上面的 `(.+)` 替换为 `(sl\d+)`。


我在这里使用了临时重定向（HTTP 302）。这能让我稍后更新目标 URL。如果希望短链接始终指向同一目标，则可以使用永久重定向（HTTP 301）。用 `permanent` 替换第三行的 `temp`。


### 构建你的映射


编辑配置文件 `RewriteMap` 行中的指定文件。格式是空格分隔的键值存储。在每一行上放一个链接：



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

### 重启 HTTPD


最后一步是重启 HTTPD 进程。这是通过 `systemctl restart httpd` 或类似命令完成的（命令和守护进程名称可能因发行版而不同）。你的链接缩短服务现已启动并运行。当你准备编辑映射时，无需重新启动 Web 服务器。你所要做的就是保存文件，Web 服务器将获取到差异。


### 未来的工作


此示例为你提供了基本的 URL 缩短服务。如果你想将开发自己的管理接口作为学习项目，它可以作为一个很好的起点。或者你可以使用它分享容易记住的链接到那些容易忘记的 URL。




---


via: <https://opensource.com/article/18/7/apache-url-shortener>


作者：[Ben Cotton](https://opensource.com/users/bcotton) 选题：[lujun9972](https://github.com/lujun9972) 译者：[geekpi](https://github.com/geekpi) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
Long ago, folks started sharing links on Twitter. The 140-character limit meant that URLs might consume most (or all) of a tweet, so people turned to URL shorteners. Eventually, Twitter added a built-in URL shortener ([t.co](http://t.co)).

Character count isn't as important now, but there are still other reasons to shorten links. For one, the shortening service may provide analytics—you can see how popular the links are that you share. It also simplifies making easy-to-remember URLs. For example, [bit.ly/INtravel](http://bit.ly/INtravel) is much easier to remember than [https://www.in.gov/ai/appfiles/dhs-countyMap/dhsCountyMap.html](https://www.in.gov/ai/appfiles/dhs-countyMap/dhsCountyMap.html). And URL shorteners can come in handy if you want to pre-share a link but don't know the final destination yet.

Like any technology, URL shorteners aren't all positive. By masking the ultimate destination, shortened links can be used to direct people to malicious or offensive content. But if you surf carefully, URL shorteners are a useful tool.

We [covered shorteners previously](https://opensource.com/article/17/3/url-link-shortener) on this site, but maybe you want to run something simple that's powered by a text file. In this article, we'll show how to use the Apache HTTP server's mod_rewrite feature to set up your own URL shortener. If you're not familiar with the Apache HTTP server, check out David Both's article on [installing and configuring](https://opensource.com/article/18/2/how-configure-apache-web-server) it.

## Create a VirtualHost


In this tutorial, I'm assuming you bought a cool domain that you'll use exclusively for the URL shortener. For example, my website is [funnelfiasco.com](http://funnelfiasco.com), so I bought [funnelfias.co](http://funnelfias.co) to use for my URL shortener (okay, it's not exactly short, but it feeds my vanity). If you won't run the shortener 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 shortener. For more information on VirtualHosts, see [David Both's article](https://opensource.com/article/18/3/configuring-multiple-web-sites-apache). This setup requires just a few basic lines:

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


## Create the rewrites

This service uses HTTPD's rewrite engine to rewrite the URLs. If you created a VirtualHost in the section above, the configuration below goes into your VirtualHost section. Otherwise, it goes in the VirtualHost or main HTTPD configuration for your server.

```
RewriteEngine on
RewriteMap shortlinks txt:/data/web/shortlink/links.txt
RewriteRule ^/(.+)$ ${shortlinks:$1} [R=temp,L]
```


The first line simply enables the rewrite engine. The second line builds a map of the short links from a text file. The path above is only an example; you will need to use a valid path on your system (make sure it's readable by the user account that runs HTTPD). The last line rewrites the URL. In this example, it takes any characters and looks them up in the rewrite map. You may want to have your rewrites use a particular string at the beginning. For example, if you wanted all your shortened links to be of the form "slX" (where X is a number), you would replace `(.+)`

above with `(sl\d+)`

.

I used a temporary (HTTP 302) redirect here. This allows me to update the destination URL later. If you want the short link to always point to the same target, you can use a permanent (HTTP 301) redirect instead. Replace `temp`

on line three with `permanent`

.

## Build your map

Edit the file you specified on the `RewriteMap`

line of the configuration. The format is a space-separated key-value store. Put one 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 last step is to restart the HTTPD process. This is done with `systemctl restart httpd`

or similar (the command and daemon name may differ by distribution). Your link shortener is now up and running. When you're ready to edit your map, you don't need to restart the web server. All you have to do is save the file, and the web server will pick up the differences.

## Future work

This example gives you a basic URL shortener. It can serve as a good starting point if you want to develop your own management interface as a learning project. Or you can just use it to share memorable links to forgettable URLs.

## 1 Comment