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

last updated in Categories 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 you will be automatically redirected to 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:
  2. URL shortener domain name:
  3. Blog url: (Post ID 100)
  4. URL Shortener url: (Post ID 100)

How does it work?

  • First, user type or share url
  • User will be automatically redirected to using mod_rewrite.
  • WordPress will automatically convert into a pretty permalinks such as 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 can be generated and shared using facebook/twitter.

Step #1 Get a domain name

Get a short domain name such as 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 points to
Sample outputs: 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]+)$" => "$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]+)$$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]+)$$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="' .$id. '&amp;t=' .the_title('','',false).  '" target="_blank">Share on Facebook</a>';
 echo '<a title="Tweet this post" href="' .the_title('','',false). '' .$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


Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

5 comment

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

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

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

    Have a question? Post it on our forum!