有人问我短网址到底怎么做,自己能做吗?我说可以而且原理非常简单?下边是我的代码和配置分享出来,叫你2分钟急速编程做短网址
win8的iis8的 URL rewrite 支持iis7
[php]
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="url">
<match url="^(.*)a(.*)$" />
<action type="Rewrite" url="{R:1}/index.php?a={R:2}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
[/php]
iis6的
[php]
[ISAPI_Rewrite]
# 3600 = 1 hour
CacheClockRate 3600
RepeatLimit 32
# Protect httpd.ini and httpd.parse.errors files
# from accessing through HTTP
RewriteRule ^(.*)a(.*)$ $1/index\.php\?a$2
[/php]
apache的伪静态
[php]
# 将 RewriteEngine 模式打开
RewriteEngine On
# Rewrite 系统规则请勿修改
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^a(.*)$ index.php?a=$1
[/php]
程序代码
[php]
<?php
/*
*CREATE TABLE `url_demo` (
* `id` int(11) NOT NULL AUTO_INCREMENT,
* `durl` varchar(9) NOT NULL,
* `zurl` varchar(250) NOT NULL,
* PRIMARY KEY (`id`)
* ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
*
*/
$conn = mysql_connect("localhost","root","");
mysql_select_db("test",$conn);
$u = $_GET[a];
$sql = "select zurl from url_demo where durl = '".$u."'";
$data = mysql_query($sql);
$result = mysql_fetch_array($data);
if($result['zurl']!==""){
header("Location:{$result['zurl']}");
}
//生成随机函数
function create_randomstr($lenth = 6) {
return random($lenth, '123456789bcdefghijklmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ');
}
function random($length, $chars = '0123456789') {
$hash = '';
$max = strlen($chars) - 1;
for($i = 0; $i < $length; $i++) {
$hash .= $chars[mt_rand(0, $max)];
}
return $hash;
}
$durl = create_randomstr(6);
$zurl = 'http://www.325.hk';
$sr = mysql_query("insert into url_demo (durl,zurl) value ('{$durl}','{$zurl}')");
if($sr){
echo "您的短网址是http://localhost/a".$durl;
}
[/php]
短网址源码:
http://eps.gs/aJE
短网址地址:
http://eps.gs 日期:2018年03月05日 20:34:14 星期一 分类:
好文分享 浏览(37182)
本文地址:https://www.blogs.hk/post-3672.html [
百度已收录]
声明:本页信息由网友自行发布或来源于网络,真实性、合法性由发布人负责,请仔细甄别!本站只为传递信息,我们不做任何双方证明,也不承担任何法律责任。文章内容若侵犯你的权益,请联系本站删除!
留言咨询