≡ Menu

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

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
host io9.in
Sample outputs:

io9.in has address

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. '&amp;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.


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

Share this on:

Your support makes a big difference:
I have a small favor to ask. More people are reading the nixCraft. Many of you block advertising which is your right, and advertising revenues are not sufficient to cover my operating costs. So you can see why I need to ask for your help. The nixCraft, takes a lot of my time and hard work to produce. If you use nixCraft, who likes it, helps me with donations:
Become a Supporter →    Make a contribution via Paypal/Bitcoin →   

Don't Miss Any Linux and Unix Tips

Get nixCraft in your inbox. It's free:

{ 5 comments… add one }
  • Rilwis May 4, 2012, 9:53 am

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

  • Joy May 19, 2012, 10:39 am

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

  • nixCraft May 19, 2012, 4:58 pm


    You should use latest version – YOURLS 1.5.

  • Sharath December 18, 2014, 7:09 am

    Thanks for the detailed steps.. Let me try YOURLS in one of my site.

  • Ada Rawson January 11, 2015, 10:14 am

    It’s amazing in support of me to have a site, which is valuable designed for my knowledge. thanks admin

Leave a Comment

   Tagged with: , , , , , , , , , ,