短網址 yourls 應用 - 新增亂數 random 網址

  • 5166
  • 0

摘要:短網址 yourls 應用 - 新增亂數 random 網址

 

上次介紹過「建立自己的短網址服務 - 使用 YOURLS - 在 CentOS 環境怎麼在 CentOS 環境下,利用 yourls 套件功能來讓你的 web server 也能提供短網址的服務,但是預設的短網址形式只有 36 跟 62 這兩種「URL Shortening settings」,形式為「http://your.domain/循序變數」,這跟一般的應用不一樣,怎麼讓你的短網址也變成「http://your.domain/random」。

也許上面的文字說明不太清楚,我們來看例子比較容易理解

 

原本的的短網址依序為

http://your.domain/1
http://your.domain/2
http://your.domain/3 

我們要將他變更為~底下這種後面為亂數不規則的代號

http://your.domain/4dysvs
http://your.domain/d6grer
http://your.domain/kywens

---------------------------------------------------------------

短網址有一個目的就是不要讓人家知道文件之間的關連性,原本的序號(1~N)可以讓別人很容易的猜到其他短網址的路徑,這不是大家所要的,於是我們需要安裝額外的「random-keywork」 plugin 套件

方法如下:

  1. cd /var/www/html/yourls/       //切換到你的 yourls 套件所放置的目錄
  2. cd user/plugin                       // 切換到 plugin 路徑
  3. sudo mkdir random-keyword    // 建立子目錄
  4. cd random-keyword   
  5. sudo vi plugin.php                 // 建立這個 plugin 所要做的動作,請將底下棕色方框裡的內容,貼到這個檔案內容,然後存檔離開。
  6. http://your.domain/yourls/admin      // 使用瀏覽器連到你的 yourls 管理介面
  7. 點選「Manage Plugins」

     
  8. 然後把 Random Keywords 啟動(active)

     

 


<?php
/*
Plugin Name: Random Keywords
Plugin URI: http://yourls.org/
Description: Assign random keywords to shorturls, like bitly (sho.rt/hJudjK)
Version: 1.1
Author: Ozh
Author URI: http://ozh.org/
*/

/* Release History:
*
* 1.0 Initial release
* 1.1 Added: don't increment sequential keyword counter & save one SQL query
* Fixed: plugin now complies to character set defined in config.php
*/

global $ozh_random_keyword;

/*
* CONFIG: EDIT THIS
*/

/* Length of random keyword */
$ozh_random_keyword['length'] = 5;

/*
* DO NOT EDIT FARTHER
*/

// Generate a random keyword
yourls_add_filter( 'random_keyword', 'ozh_random_keyword' );
function ozh_random_keyword() {
        global $ozh_random_keyword;
        return yourls_rnd_string( $ozh_random_keyword['length'] );
}

// Don't increment sequential keyword tracker
yourls_add_filter( 'get_next_decimal', 'ozh_random_keyword_next_decimal' );
function ozh_random_keyword_next_decimal( $next ) {
        return ( $next - 1 );
}

 

這樣就搞定啦,web 也不用重開~~頂多把 yourls 的 admin 畫面 refresh 一下即可,以後產出的路徑就變成了「http://your.domain/random_keyword」了。 

 

如果你還想裝看看 plugin 套件,你可以參考「https://github.com/YOURLS/YOURLS/wiki/Plugin-List」,裡頭還有很多套件可供選用。

 

Reference: https://github.com/YOURLS/YOURLS/wiki/Plugin-%3D-Random-Keywords

~End