为什么要用短地址
短地址的兴起主要是twitter和微博这类网络社交媒体对字数限制,140个字本来就说不了几句话,再放个长长的url,基本就写不了什么内容了。其实微博和百度都提供免费稳定的短地址服务,今天主要针对爱捣鼓,喜欢折腾,希望有自己个性短地址的用户,如何自建自己的短地址服务。
短地址的原理
短地址服务其实就是利用http301重定向服务,把系统生成的短地址重定向到指定的长地址去,短地址同时可以方便的统计发出的链接的投放效果,所以被广泛使用。
关于YOURLS
感谢开源!YOURLS是个强大的短地址服务程序,并且提供api接口,所有人都可以拿来开发定制自己的服务,今天主要说YOURLS服务的安装和基础使用
第一步:找个合适的域名
要做短地址,那域名一定要短,.cn的短地址基本很难找到了,但是其实还有很多带域名是比较好注册的,例如我们这里注册了一个17h.pw的短域名,第一年9块钱,就拿它来做短地址服务了。
第二步:域名解析
第三步:服务器网站建设
1、创建网站根目录,这里用的宝塔后台做管理,简单方便,具体操作可以看之前的文章谷歌云快速搭建wordpress站点,宝塔在创建站点时可以同时创建数据库。
2、上传程序文件
下载最新的程序文件 https://github.com/YOURLS/YOURLS ,上传至刚刚创建的网站根目录
第四步:conf配置
进入网站目录/user/文件夹下,将config-sample.php文件重命名为config.php。
修改config文件内容,如下:
先进性数据库设置
/** MySQL database username你的数据库用户名 */ define( 'YOURLS_DB_USER', 'your db user name' ); /** MySQL database password你的数据库密码 */ define( 'YOURLS_DB_PASS', 'your db password' ); /** The name of the database for YOURLS 数据库名*/ define( 'YOURLS_DB_NAME', 'yourls' ); /** MySQL hostname. ** If using a non standard port, specify it like 'hostname:port', eg. 'localhost:9999' or '127.0.0.1:666' 数据库地址*/ define( 'YOURLS_DB_HOST', 'localhost' ); /** MySQL tables prefix 数据库前缀*/ define( 'YOURLS_DB_PREFIX', 'yourls_' );
然后是站点设置
/** YOURLS installation URL -- all lowercase, no trailing slash at the end. ** If you define it to "http://sho.rt", don't use "http://www.sho.rt" in your browser (and vice-versa) 你的域名*/ define( 'YOURLS_SITE', 'http://your-own-domain-here.com' ); /** Server timezone GMT offset时区设置 */ define( 'YOURLS_HOURS_OFFSET', 0 ); /** YOURLS language ** Change this setting to use a translation file for your language, instead of the default English. ** That translation file (a .mo file) must be installed in the user/language directory. ** See http://yourls.org/translations for more information 语言设置*/ define( 'YOURLS_LANG', '' ); /** Allow multiple short URLs for a same long URL ** Set to true to have only one pair of shortURL/longURL (default YOURLS behavior) ** Set to false to allow multiple short URLs pointing to the same long URL (bit.ly behavior) 是否允许多个短地址对应同一长地址*/ define( 'YOURLS_UNIQUE_URLS', true ); /** Private means the Admin area will be protected with login/pass as defined below. ** Set to false for public usage (eg on a restricted intranet or for test setups) ** Read http://yourls.org/privatepublic for more details if you're unsure 是否开放使用(是否需要用户名密码才能使用)*/ define( 'YOURLS_PRIVATE', true ); /** A random secret hash used to encrypt cookies. You don't have to remember it, make it long and complicated. Hint: copy from http://yourls.org/cookie 这里需要一段加密cookie的随机数,可以去前面的网址随机生成**/ define( 'YOURLS_COOKIEKEY', 'modify this text with something random' ); /** Username(s) and password(s) allowed to access the site. Passwords either in plain text or as encrypted hashes ** YOURLS will auto encrypt plain text passwords in this file ** Read http://yourls.org/userpassword for more information 你的用户名和密码*/ $yourls_user_passwords = array( 'username' => 'password', // 'username2' => 'password2', // You can have one or more 'login'=>'password' lines ); /** Debug mode to output some internal information ** Default is false for live site. Enable when coding or before submitting a new issue 调试模式 */ define( 'YOURLS_DEBUG', false );
最后是关于短地址服务的基础设置
/** URL shortening method: 36 or 62 缩短方式36为数字和小写字母组合,62为数字和大小写字母组合*/ define( 'YOURLS_URL_CONVERT', 36 ); /* * 36: generates all lowercase keywords (ie: 13jkm) * 62: generates mixed case keywords (ie: 13jKm or 13JKm) * Stick to one setting. It's best not to change after you've started creating links. */ /** * Reserved keywords (so that generated URLs won't match them) * Define here negative, unwanted or potentially misleading keywords.屏蔽关键词 */ $yourls_reserved_URL = array( 'porn', 'faggot', 'sex', 'nigger', 'fuck', 'cunt', 'dick', );
ok,到这里基本设置完毕,来到最后一步
伪静态设置
如果是Apache服务器,程序已经集成了.htaccess规则,如果是Nginx,添加如下伪静态规则:
location / { try_files $uri $uri/ /yourls-loader.php?$args; }
把.htaccess也附上吧
# BEGIN YOURLS <IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^.*$ /yourls-loader.php [L] </IfModule> # END YOURLS
使用
访问:域名/admin,进入后台,输入你刚才的用户名密码,进入后就可以开始使用了。
汉化及插件安装
汉化可以去下载网友提供的汉化包,https://github.com/guox/yourls-zh_CN/ 上传到user/languages目录,然后修改下config文件:
/** YOURLS language ** Change this setting to use a translation file for your language, instead of the default English. ** That translation file (a .mo file) must be installed in the user/language directory. ** See http://yourls.org/translations for more information */ define( 'YOURLS_LANG', 'zh_CN' );
插件的安装直接去https://github.com/YOURLS/awesome-yourls 看,都有详细说明,下载到/user/plugins/ 插件名 / 目录下,在后台激活即可。