Roshan Book

My Tech Notebook

Monthly Archives: April 2013

Creating a TinyURL with TinyURL’s API


function createTinyUrl($strURL) {
    $tinyurl = file_get_contents("".$strURL);
    return $tinyurl;
To call this function

How To Create Your Own Tinyurl Style URL Shortener

A URL shortener is a service that takes in a long URL and  gives you a short and memorable URL that you can distribute to anyone you want. The service takes care of redirecting users who land up at the shortened URL to the original URL.

TinyURL, of course, was one of the pioneers in the field but ever since Micro blogging sites like Twitter started to take off, a number of services have come up that all provide essentially the same URL shortening service.

One major problem with using a third-party URL shortening service is that all your links are always bound to that one service and if they ever go out of business, like did, all your data is gone too. And that is why, if you’re serious about saving all the wonderful links that you’ve discovered from around the web, you might want to consider running your own URL shortening service.

YOURLS gives you exactly that.


First of all, you need to download YOURLS to your web server and unzip the package.


Now, use your favorite tool to create a mysql database for YOURLS. If you’re using a shared hosting provider, there must be a way to do this from inside your domain control panel. If not, just open a ticket with your hosting provider and let them handle it for you. I named my database ‘kunz_yourls‘.

Now, browse over to the includes directory and rename the file named ‘config-sample.php’ to ‘config.php’.

Open ‘config.php’ in a text editor and edit the following variables.


The comments in the file are pretty self explanatory and you can also get an overview here.


Now, from your web browser, visit the site http://<website>/admin, where website is the domain name where you installed YOURLS. Click the “Install YOURLS” button.

If all went well, you will see the following message which means that YOURLS was successfully installed on your domain.


Start creating your own Short URLs

Browse over to http://<website>/admin/index.php and login using the username/password combination that you entered earlier in config.php. If you didn’t enter any, try username/password. That’s the default combination and that is why you should change it when you’re installing YOURLS.

Upon login, you’ll be able to create short URLs easily by just entering the complete URL in the prominently displayed text box and clicking “Shorten the URL” button.


You can even get some nice and simple statistics about the short URLs that you’ve created.


Be sure to check out the bundled tools. You get two cool bookmarklets that you can use to create short URLs right from your browser toolbar.

YOURLS also provides an API that you can take advantage of in custom applications and such.

Did you find YOURLS useful? Will you use it over, say, Let us know in the comments.

Redirecting non-www to www with .htaccess

Source :

If you want to redirect all non-www requests to your site to the www version, all you need to do is add the following code to your .htaccess file:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

This will redirect any requests to to There are several benefits from doing that:

  • It will avoid duplicate content in Google
  • It will avoid the possibility of split page rank and/or split link popularity (inbound links).
  • It’s nicer, and more consistent.

Note that if your site has already been indexed by Google without the www, this might cause unwanted side effects, like lost of PR. I don’t think this would happen, or in any case it would be a temporary issue (we are doing a permanent redirect, 301, so Google should transfer all rankings to the www version). But anyway, use at your own risk!

Something nice about the code above is that you can use it for any website, since it doesn’t include the actual domain name.

Redirecting www to non-www

If you want to do the opposite, the code is very similar:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^my-domain\.com$ [NC]
RewriteRule ^(.*)$$1 [R=301,L]

In this case we are explicitly typing the domain name. I’m sure it’s possible to do it in a generic way, but I haven’t had the time to work one out and test it. So remember to change ‘my-domain’ with your domain name!

%d bloggers like this: