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

See my sample single.php here.


A sample tiny url sharing on Twitter:

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

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 5 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersdf duf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Modern utilitiesbat exa
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg glances gtop jobs killall kill pidof pstree pwdx time vtop
Searchingag grep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
5 comments… add one
  • Rilwis May 4, 2012 @ 9:53

    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

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

  • 🐧 nixCraft May 19, 2012 @ 16:58


    You should use latest version – YOURLS 1.5.

  • Sharath Dec 18, 2014 @ 7:09

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

  • Ada Rawson Jan 11, 2015 @ 10:14

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

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum