Redirect non-www domain to www using htaccess for seo

If you own a website running under your own domain, then you probably already understand the importance of SEO. One of the easiest and most important things to improve your SEO health is to avoid duplicate links in your content. So, make sure that you are not serving both and links separately. Ideally you should be 301 redirecting one to another (for understanding different redirects, check out seomoz article) to not dilute your SEO karma between www and non-www links.

I will just cover how I did it on webfaction. To check out how to do this on any other hosting provider, you can google ‘301 redirect htaccess <yourhostname>’

On your webfaction control panel, do the following-

  1. Create a new Static/CGI/PHP App – let’s call it dummyapp.
  2. Create a new website mapping to dummyapp. If you were mapping both www and non-www domain to your working app before, you will need to remove the non-www domain from that app first. You should now have something like below where festsy is the working app and dummyfestsy the new app we created only for redirects.
  3. Now, ssh to your webfaction root and go to webapps/dummyapp/ Remove any file that exists there (such as index.html). Now add a .htaccess file with just one line – Redirect 301 / Do NOT forget the trailing slash at the end else your extended urls won’t work. Save and close.
  4. Go to a browser and test It should redirect to Also try an extended url to be safe e.g. in my case. If you set up the trailing slash correctly in previous step, this should redirect to

And, that’s it! I found this to be the easiest way for redirections. Hope it helps.

Launching toy prototype on Heroku!

I have been implementing few ideas in Django for past month or so but as exciting it might be to see your code working wonders on localhost, you really want to see it running live. Over last week, I explored various hosting options for Django and after reading a ton of posts and talking to few people, I picked Heroku. I also contemplated paying someone for it but I thought that investing a week in learning this is much more valuable and reusable than if I have to find and hire someone, explain my requirements (do it for every new project) and following up to make sure they are doing it right. Basically, it had come down to these 3 options-

  1. Webfaction – the de facto standard for a lot of python developers. It has good tutorials and seemed like the most straightforward guided hosting option.
  2. Linode – I talked to a friend who does professional django consulting and development and this is what he uses. But I figured that linode is more suited for professionals who know exactly how they want to setup their environment. Linode gives you most freedom if you know what you want but it can be overwhelming for novices. You are paying and you have to work.
  3. Heroku – the newcomer and the revered choice for rails application has finally embraced django. Heroku takes away all your hassles of configuring a prod environment and makes the deployment as easy as humanly possible. You are paying so that you work least. It does not mean it was all magical, you still need to understand what a good environment is and what are good prod practices. But I figured you need to learn those with any platform anyways, so why not invest some time in learning this growing service once and for all. It is probably very straightforward to people who have done it before (and I read a lot of pros switching to Heroku) but I spent 2 days figuring out minute details which even got frustrating at some points. But, in the end, I have a minimum working prototype up on web and I am loving it.

If I had to summarize past few weeks, it would be like this:

Get fairly comfortable with Django and already tried out few tutorials.
Learn the importance of good URLs.
How to start with minimal features and structure (urls) and fill out views later.
Always reuse opensource libraries – userena, imagekit etc.
Working with images can be a pain – getting PIL to work with jpeg was a nightmare but essential.
Understand the meaning and difference between static and media URLs (it took me unbelievably long time).
You are lucky to be developing in post twitter-bootstrap era. CSS will never have to be ugly now and anyone can design a professional looking web app.
Beauty of django ORM is that I can use mysql in dev and postgresql freely supported on heroku (use south for migrations).
Git is godsend, learn it asap.
Heroku needs virtualenv, so learn it.
Deploying my toy app on heroku cost me $0 so far (its non-persistent and slow but I don’t care right now). I can keep iterating and getting feedback from friends while adding those few missing components needed to launch the project live.
Internet has all the answers, just be persistent and think what is the right question.

The bottom line is learn whatever it takes to go live!

Anyways, here is my toy prototype live – (the picture links get broken in sometime due to non-persistence)
The fun has just begun, more updates soon.

Setting up Django with MySql – with and without MAMP

In this post, we will discuss the steps needed to get your django work with mysql database  on Mac OS Lion – there are 2 ways you can use mysql. It comes packaged with MAMP and if you are already using it, you can configure mysql within MAMP to get running with django. The advantage is MAMP’s front end controls and built in phpmyadmin. Otherwise, you can install both mysql and phpmyadmin fresh.

But before that, you should install python and django as covered nicely in Django Book. What it doesn’t cover in detail is getting the database setup, so refer to the following once you reach ‘Setting up the database’ section.

  1. First, we need MySQL server. If you already have MAMP installed, you dont need this step, skip over to next. If you dont, lets install MySQL community server from I downloaded the then latest mysql-5.5.20-osx10.6-x86_64.dmg and followed the instructions. If you set the preferences correctly in Startup Package and Preferences Pane (double click MySQLStartupItem.pkg and then MySQL.prefPane), you can configure mysql to be started/stopped from System Preferences. We did this to have easy controls to start and stop mysql.
  2. Setup mysql-python package– Although you have installed mysql, we need MySQLdb package to allow it to work with python. I have heard you can use pip or easy_install to install this package easilybut it can start giving errors sometime. So, here is the longer route but one which I have successfully tried and hopefully you can too. First, get mysql developer package to have all the files-
    • From the same mysql download url, download the latest tar.gz package (version should be same as the dmg file you downloaded in step 1). Put it in your home dir and execute following command-cd ~; tar -xvzf mysql-5.5.20-osx10.6-x86_64.tar.gz
    • This will create a dir called mysql-5.5.20-osx10.6-x86_64. Copy this dir over to /usr/local using the command sudo cp -r mysql-5.5.20-osx10.6-x86_64 /usr/local/
    • Now, rename this dir in /usr/local to mysql using
      sudo mv -r /usr/local/mysql-5.5.20-osx10.6-x86_64 /usr/local/mysql
      With this, we have created a hard copy of our latest mysql. If you upgrade your mysql in future, follow the same steps. Alternatively, you can create a symlink too.
    • Add this to your .bash_profile (created in previous post in your home dir) so that your system will know where to find mysql related files –
      export PATH=$PATH:/usr/local/mysql/bin

    Next, install mysql-python package-

    • Download the latest MySQL-python package from to your home dir
      Execute following commands-
      cd ~; tar -xvzf MySQL-python-1.2.3.tar.gz
      cd MySQL-python-1.2.3
    • Edit site.cfg on vi and change the mysql path as follows
      mysql_config = /usr/local/mysql/bin/mysql_config
    • Lets build and install MySQL-python package now:
      sudo python build
      sudo python install
    • IMPORTANT: Edit your ~/.bash_profile, add the following line:
      export DYLD_LIBRARY_PATH=/usr/local/mysql/lib/
  3. Lets test our setup to make sure everything is working as expected-
    On terminal, type python to start python shell. On python shell, type ‘import MySQLdb’. If you see no errors, you are good to go!

The steps 2-3 are required both with and without MAMP. Next, few helpful things-

  1. with MAMP – if you have MAMP, you already have phpMyAdmin installed and instead of starting MySql through system preferences, you can control it from MAMP interface. Further, I have these in my .bash_profile to use mysql from MAMP-
    alias mysql=/Applications/MAMP/Library/bin/mysql
    alias mysql_config=/Applications/MAMP/Library/bin/mysql_config
    Other thing to note, when you will setup your own django project, use this in -‘HOST’: ‘/Applications/MAMP/tmp/mysql/mysql.sock’
  2. without MAMP – lets install phpMyAdmin to manage our mysql server using instructions given here. I tried and it worked great, so I dont want to duplicate the efforts. Lastly, in your, you will leave ‘HOST’ empty when you setup your own django project.

So, are there any particular advantage of using mysql with or without MAMP? – I dont think so. I used both of them and did not feel any difference worth worrying over. So, take your pick – if you have MAMP, use it. If you dont, you definitely dont need it for django.

See previous article on Getting started to code with Python/Django – Learn Python/Django and get ready to launch your own idea on Internet

In next and last article, I will post how I learned the rest and got to build my own django app.

Learn Python/Django and get ready to launch your own idea on Internet

You have ideas but you never got to create something because you are not a developer or specifically a web developer. That has become such an ubiquitous problem that its not an acceptable excuse anymore. Thankfully to the rapidly evolving web frameworks, you dont need to know a ton lot to create your own application prototypes. I have programming background but never did web development before until 12 days ago. Since it was really not as difficult as one expects, I thought I’ll share the experience and encourage aspiring entrepreneurs, web developers to try it for themselves.

Django or Rails – If you have never heard the word Django before, don’t fret – its a web framework for Python which is incidentally evolving very fast and possibly stealing people away from Ruby on Rails. You can read this discussion on Django vs Rails on Quora if you are undecided between the two. I had tried Rails before (following the excellent tutorial from Michael Hartl) just to get a feel of all the hype surround RoR and found it great at that time. I never delved deeper as I was not trying to actively build something but when I tried Django over past few days, I can definitely say that Python is easier to learn than Ruby and Django offers several advantages over Rails – biggest for me is the ready-made admin interface. More and more companies are leaning towards Python/Django now and if you have to take a pick, I bet Django is the way to go.

I’ll document how I learned Django to create a standalone application over a series of posts. Since I am familiar with object oriented programming, linux command line and mysql before, I did it at faster speed but even if you dont, you can do this in few weeks.

Overall, this was my learning path:

  • Day 1 – Setting up Django on your laptop
  • Day 2-5 – Reading and working the examples given in Django book
  • Day 6-10 – Start following Django 1.0 Website Development by Ayman Hourieh and apply the steps in building prototype of your own idea (if you do not have an idea yet, simply follow his examples)
  • Next – Add other functionality to your idea as needed (google for solutions when you are stuck) + work on design (which is a whole new beast)

This first post is going to cover ‘getting started on Mac’ (isn’t that what everyone who is interested in this post using now anyways?)-

Setup Terminal – I recommend that you setup a happy coding environment (terminal) before you begin – this can make a lot of difference in your motivation and desire to proceed especially when you are frustrated on being stuck somewhere. If you have never used Terminal before, you can read a crash course. Terminal offers various theme settings, I recommend using one with dark background – I personally use Pro (semi transparent black background and white text).

Just learn the basic cd, ls, mkdir, ln, chmod, sudo commands and try to be comfortable with some text editor – I recommend vi (if you have never ever done this, spend 2-4 days learning to enjoy this). Now, the happy part, add this to your .bash_profile (if it doesn’t exist, create one using sudo vi ~/.bash_profile)

# configure my multi-line prompt
==> \e[m’

It should make your prompts colorful, mine looks like this-

Next, I wanted a good coloring scheme for vi because you will be spending a lot of time writing and editing code inside vi editor (if you choose to go along with it). So, I added this to my .vimrc (create one if needed – vi ~/.vimrc)-

colo desert
syntax on
set number
set expandtab tabstop=2 shiftwidth=2

And now, my vi editor is colorful, highlights the syntax, shows line numbers (helpful in debugging when you get an error), sets tab at 2 spaces as used in many coding examples online –

And that can make a lot of difference when you are spending hours on command line following the examples from django books. I have barely touched anything in depth but with these minimal efforts, you can get going. If you have any questions, leave a comment and I’ll try to help.

Next post will cover setting up Django and mysql on Mac. If you want to read some other helpful posts meanwhile, try Eddy Chan’s How I learnt Python Django.

Update: Read next post – Setting up Django and MySql with and without MAMP here.


5 years of Twitter

Jack Dorsey, co-founder of Twitter, is posting interesting tweets on initial development days of Twitter. What an amazing feeling it must be to see their experiment rocket like this. If this doesn’t inspire you to start working on your crazy idea, what will?

[blackbirdpie url=”!/jack/status/48076297083158529″]

[blackbirdpie url=”!/jack/status/48074436380856320″]

[blackbirdpie url=”!/jack/status/48066896033091585″]