url shortener

How to Make a URL Shortener Site with YOURLS and Drupal

YOURLS is a simple and easy to use URL shortener, but leaves much needed when it comes to page layout and usefulness as compared to a Content Management System.

For this reason, I believe that Drupal is great for being used in combination with YOURLS.

Setting Up YOURLS

  1. Unzip the YOURLS archive
  2. Copy includes/config-sample.php to includes/config.php
  3. Open includes/config.php with a raw text editor (like Notepad) and fill in the required settings
  4. Upload the unzipped files to your domain public_html or www folder
  5. Create a new database (see Configuration – you can also use an existing one)
  6. Point your browser to http://yoursite.com/admin/

Installing Drupal in a subdirectory

  1. http://drupal.org/requirements
  2. Download Drupal
  3. Extract
  4. Upload it to a subdirectory, such as /content or /pages
  5. Grant write permissions on the configuration file
  6. Create the database
  7. Run the install script
  8. Clean URLs

Drupal + YOURLS

Now that you have them both installed, add the following to your .htaccess

DirectoryIndex content

replace content with the name of the subdirectory you used.

Rename /sample-public-front-page.php to something like url.php

Go to your drupal install, login, and go to /subdomain/node/add/page

Create a new page titled URL, make sure to select PHP as the Input Format, and insert the following code

<?php
require_once( dirname(__FILE__).’/includes/load-yourls.php’ );

// Part to be executed if FORM has been submitted
if ( isset($_REQUEST[‘url’]) ) {

$url = $_REQUEST[‘url’];
$keyword = isset( $_REQUEST[‘keyword’] ) ? $_REQUEST[‘keyword’] : ” ;

$return = yourls_add_new_link( $url, $keyword );

$shorturl = $return[‘shorturl’];
$message = $return[‘message’];

echo <<<RESULT
<h2>URL has been shortened</h2>
<p>Original URL: <code><a href=”$url”>$url</a></code></p>
<p>Short URL: <code><a href=”$shorturl”>$shorturl</a></code></p>
<p>$message</p>
RESULT;

<?php yourls_share_box( $url, $shorturl ); ?>

} else {

echo <<<HTML
<h2>Enter a new URL to shorten</h2>
<form method=”post” action=””>
<p><label>URL: <input type=”text” name=”url” value=”http://” size=”50″ /></label></p>
<p><label>Optional custom keyword: <input type=”text” name=”keyword” size=”5″ /></label></p>
<p><input type=”submit” value=”Shorten” /></p>
</form>
HTML;

}

?>

Now publish it as sticky to the front page.

That’s it!

Now of course there will be lots of things you need to customize, and make look nice, and maybe some things I forgot. But remember, Google is your friend.