How to make a URL Shortener 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
- Unzip the YOURLS archive
- Copy
includes/config-sample.phptoincludes/config.php - Open
includes/config.phpwith a raw text editor (like Notepad) and fill in the required settings - Upload the unzipped files to your domain
public_htmlorwwwfolder - Create a new database (see Configuration – you can also use an existing one)
- Point your browser to
http://yoursite.com/admin/
Installing Drupal in a subdirectory
- http://drupal.org/requirements
- Download Drupal
- Extract
- Upload it to a subdirectory, such as /content or /pages
- Grant write permissions on the configuration file
- Create the database
- Run the install script
- 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.
Recent Comments
- JrrMaster on YOURLS Plugin – Popular Links
- tj on YOURLS Plugin – Popular Links
Archives
- April 2012
- March 2012
- February 2012
- January 2012
- December 2011
- November 2011
- October 2011
- September 2011
- August 2011
- July 2011
- June 2011
- April 2011
- February 2011
- January 2011
- December 2010
- November 2010
- October 2010
- July 2010
- June 2010
- May 2010
- April 2010
- March 2010
- February 2010
- January 2010
- December 2009
- November 2009
- October 2009
- September 2009
- August 2009
- July 2009
- June 2009
- May 2009
- April 2009
- March 2009
- February 2009
- January 2009
- December 2008
- November 2008
- October 2008
- September 2008
- April 2008
- March 2008
- February 2008
- January 2008
- December 2007
- November 2007
- October 2007
- September 2007
Categories





