Create Custom URL Shortener For WordPress Based Blog In a Five Minutes

by on May 3, 2012 · 3 comments· LAST UPDATED June 22, 2012

in Apache, lighttpd, php

You can create URL redirection service for your blog within five minutes using nothing but web server's mod_redirect module. For example, when you type or share a url io9.in/t/5159 you will be automatically redirected to http://www.cyberciti.biz/tips/my-10-unix-command-line-mistakes.html. In this quick post I will explain how to create url shortener and integrate your wordpress based blog without installing any new scripts.

Our sample setup

  1. Blog domain name: cyberciti.biz
  2. URL shortener domain name: io9.in
  3. Blog url: www.cyberciti.biz/blog/slug-here.html (Post ID 100)
  4. URL Shortener url: io9.in/100 (Post ID 100)

How does it work?

  • First, user type or share url io9.in/100
  • User will be automatically redirected to www.cyberciti.biz/blog/?p=100 using mod_rewrite.
  • WordPress will automatically convert www.cyberciti.biz/blog/?p=100 into a pretty permalinks such as www.cyberciti.biz/blog/slug-here.html. The URL to each post should be permanent, and never change.
  • You need to modify your wordpress theme code so that tiny url such as io9.in/100 can be generated and shared using facebook/twitter.

Step #1 Get a domain name

Get a short domain name such as io9.in with your favorite domain registrar. Before you can get your url shortner up and running, you'll need to associate your domain name with web server IP address. In other words, make sure domain name point to your web server IP address. In this example io9.in points to 75.126.153.213:
host io9.in
Sample outputs:

io9.in has address 75.126.153.213

Step #2 Configure mod_rewrite

I'm assuming that the web server is configured with mod_rewrite which is a URL rewriting engine for the web server. A rewrite engine modifies a web URL's appearance. This modification is called URL rewriting. Rewritten short URLs provide shorter links to blog posts. You need to edit your Apache / lighttpd / nginx web server configuration so that all incoming requests will be send to your blog.

Lighttpd web server redirect rule

Edit lighttpd.conf file and update it as follows:

## send http status code 301 with redirect ##
url.redirect = ( "^/([0-9]+)$" => "http://www.cyberciti.biz/blog/?p=$1" )

Save and close the file. Reload lighttpd web server, enter:
# /etc/init.d/lighttpd reload

Nginx web server redirect rule

Edit nginx.conf file and update it as follows:

## permanent redirect with http status code 301 ##
rewrite  ^/f/([0-9]+)$ http://www.cyberciti.biz/blog/?p=$1 permanent;

Save and close the file. Reload nginx web server, enter:
# /usr/local/nginx/sbin/nginx -s reload

Apache web server redirect rule

Edit httpd.conf or .htaccess file and update it as follows:

## send http status code 301 with redirect ##
RewriteEngine on
RewriteRule  ^/([0-9]+)$ http://www.cyberciti.biz/blog/?p=$1 [R=301,L]

Save and close the file. Reload apache web server, enter:
# apachectl -k graceful

Step #3: Modify your wordpress theme

You need to share your tiny url on Facebook or Twitter. You need to edit single.php (located in wp-content/themes/YOUR-THEME-NAME/ directory) - the single post template. This file is used when a single post is queried.

How do I generate tiny url code?

Edit single.php and within the loop add the following code:

 global $post;
 $id = $post->ID;
 echo 'Share this with other sys admins:';
 echo '<a title="Share this post on Facebook" href="http://www.facebook.com/sharer.php?u=http://io9.in/' .$id. '&t=' .the_title('','',false).  '" target="_blank">Share on Facebook</a>';
 echo '<a title="Tweet this post" href="http://twitter.com/?status=' .the_title('','',false). ' http://io9.in/' .$id. '" target="_blank">Share on Tweeter</a>';

Sample outputs:

Fig.01 Sharing blog post on Facebook and Twitter

Fig.01 Sharing blog post on Facebook and Twitter

See my sample single.php here.

Result

A sample tiny url sharing on Twitter:

Fig.02: Twitter tiny url sharing

Fig.02: Tiny url sharing on twitter

A note about URL shortening service using php scripts

You can use YOURLS software which is a small set of PHP scripts that will allow you to run your own URL shortening service . You can make it private or public, you can pick custom keyword URLs, it comes with its own API. You will love it. There's a WordPress plugin available for YOURLS, making integration with your blog a snap: create short URLs and tweet them automatically as you publish blog posts.

=> Download YOURLS

TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 3 comments… read them below or add one }

1 Rilwis May 4, 2012 at 9:53 am

Very nice tip. I’ve been using some WP plugins for shortening URLs, but the .htaccess trick is really great!

Reply

2 Joy May 19, 2012 at 10:39 am

From your suggestion I have using the YOURLS 1.4 and it is very effective. Thanks for the post.

Reply

3 nixCraft May 19, 2012 at 4:58 pm

@Joy,

You should use latest version – YOURLS 1.5.

Reply

Leave a Comment

Tagged as: , , , , , , , , , ,

Previous post:

Next post: