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
- Blog domain name: cyberciti.biz
- URL shortener domain name: io9.in
- Blog url: www.cyberciti.biz/blog/slug-here.html (Post ID 100)
- 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 188.8.131.52:
io9.in has address 184.108.40.206
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>';
See my sample single.php here.
A sample 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.Tweet itFacebook itGoogle+ itPDF itFound an error/typo on this page?