URL缩短站点

2023-09-23php开发问题
4

本文介绍了URL缩短站点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在开发一个使用 PHP、MySQL 和 Apache 的 URL 缩短站点.当我查看开源项目时,URL 缩短的总体思路:用户提供一个 URL 链接,系统从数据库中获取该链接的 ID.然后转换 ID X 基本系统(我使用的是 base 36).然后使用 Apache mod_rewrite 并创建缩短的 URL,然后重定向.是否所有的 URL 缩短站点都像这样工作,或者是否有一些使用除此之外的不同算法?有什么想法可以与其他 URL 缩短系统不同吗?

I am working on a URL shortening site which uses PHP, MySQL and Apache. General idea of URL shortening as I look at open source projects: user gives a URL link and the system gets ID for that link from database. Then convert the ID X base system (I am using base 36). Then use Apache mod_rewrite and create shortened URL and then redirect. Do all of the URL shortening sites work like that or do some use a different algorithm other than this? And any idea for making a difference from other URL shortening systems?

推荐答案

我认为你说的很对.

不过,我不会像你说的那样做的一件事是关于这部分:

One thing I would not do like you said, though, is about this part :

然后使用 apache mod_rewrite 并创建缩短网址,然后重定向.

then use apache mod_rewrite and create shorten url and then redirect.

我认为我不会创建 Apache RewriteRule,也不会使用 mod_rewrite.

I don't think I'd create an Apache RewriteRule, nor use mod_rewrite.


当收到一个短网址时,比如 short.com/MYID,Id 会:

  • 将MYID"部分解密为DB中的id号
  • 从数据库中获取 URL
  • 只需从某些服务器代码(如 PHP,使用 header 函数)

我猜有点像这样:

// fetch $urlFull from DB (corresponding to the MYID received in GET)
header('HTTP/1.x 301 Moved Permanently');
header('Location: ' . $urlFull);
die;


(编辑)如果 mod_rewrite 你的意思是transform short.com/MYID to short.com/id=MYID",哦,是的,在这种情况下,当然!


(edit) If by mod_rewrite you meant "transform short.com/MYID to short.com/id=MYID", oh, yes, in this case, of course !

顺便说一句,我在我的一个网站上使用了这样的东西:

I'm using something like this on one of my sites, btw :

RewriteEngine on
RewriteCond %{REQUEST_URI}  !^/index.php
RewriteRule ^(.*)$ /index.php?hash=$1   [L]


希望这会有所帮助:-)


Hope this helps :-)

这篇关于URL缩短站点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

PHP实现DeepL翻译API调用
DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以...
2025-08-20 php开发问题
168

PHP通过phpspreadsheet导入Excel日期数据处理方法
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的...
2024-10-23 php开发问题
287

mediatemple - 无法使用 codeigniter 发送电子邮件
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)...
2024-08-23 php开发问题
11

Laravel Gmail 配置错误
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)...
2024-08-23 php开发问题
16

将 PHPMailer 用于 SMTP 的问题
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)...
2024-08-23 php开发问题
4

关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)...
2024-08-23 php开发问题
17