Tiny post – how to use .htaccess to redirect visitors

March 22nd, 2010 by andy 3 comments »
redirect 301 /help_decide/index.html http://showcase.health2works.com/help_decide/Homepage.html

Damn, it’s been ages since i’ve posted. This is a tiny one that is simple yet very useful.

The Situation

I have a Napkee site that consists of a bunch of html files linked together. This was naturally created from a set of Balsamiq screens. Within these Balsamiq screens was a screen called ‘Homepage’ (not index, because that would have been machine friendly but user unfriendly). So, on Napkee export, no index.html file had been created, which is just messy when sending out links to the Napkee screens because i didn’t want to include Homepage.html in the link.

The Need

I wanted to redirect all requests for http://www.website.com/napkee_mockup to http://www.website.com/napkee_mockup/Homepage.html

The Options

  • [Bad] An error page: Not an option so i’ll say no more about it.
  • [Bad] A Meta Refresh, where the following code is added to the Meta tags within the file Head. Again this was not an option as it gives the redirect task to the end user’s browser.

<META HTTP-EQUIV=”refresh” content=”0;URL=http://www.new.com/new.htm”>
<META NAME=”ROBOTS” CONTENT=”NOINDEX, NOFOLLOW”>

  • [Bad] JavaScript Refresh: Too ugly to even paste the code in here, so i won’t bother. This is again bad because you’re forcing the user to not only do the redirecting for you, but you’re also demanding that they use JavaScript to do it. Not very friendly.
  • [Good] .htaccess redirect: This is the only correct way to do it.

The Solution

cd [www]/napkee_mockup

vim .htaccess

add the following:

redirect 301 /napkee_mockup/index.html http://website.com/napkee_mockup/Homepage.html

The key is to ensure that the FROM url is relative to the web application root (so you have to include /napkee_mockup/) and you shouldn’t include the domain details (http://website.com)

I hope this helps a little.

There’s more good reading at http://www.tamingthebeast.net/articles3/spiders-301-redirect.htm

How to use thor with the latest version of merb

November 26th, 2009 by andy No comments »

The latest version of thor (0.12.0) doesn’t work with the latest version of merb (1.0.15).

To use thor with your merb 1.0.15 app, you need to install thor -v0.9.9 and then follow the instructions on this page: http://wiki.merbivore.com/deployment/bundling

Merb Gem Cleanup

November 25th, 2009 by andy No comments »

It seems to be a common issue for us Merbists, who depend on multiple Gems for merb to play nicely to get into a bit of a mess as gems are updated.

So, at times a cleanup is in order. Here is what i’ve done to cleanup my merb setup:

$ sudo gem update --system
$ gem search --no-version merb | grep merb | xargs sudo gem uninstall -a # NOTE: removes all old version of merb
$ gem search --no-version dm | grep dm | xargs sudo gem uninstall -a # NOTE: removes all old version of data_mapper
$ gem search --no-version data_objects | grep data_objects | xargs sudo gem uninstall -a # NOTE: removes all old version of data_objects
$ sudo gem sources -c
$ sudo rm PATH_TO_GEMS/cache/merb* # PATH_TO_GEMS is the path to your rubygems install.  Mine is /usr/lib/ruby/gems/1.8
$ sudo rm PATH_TO_GEMS/cache/dm*
$ sudo gem install -r merb

This was taken from the meb installation instructions at: http://wiki.merbivore.com/howto/installation/gems

Call TextMate from command line

September 7th, 2009 by andy 2 comments »

A really neat feature of TextMate is the ability to call it from the command line and pass a directory or file as the object to open. Works great.

For this to work, you need to run a quick one-liner on the command line:

sudo ln -s /Applications/TextMate.app/Contents/Resources/mate /usr/local/bin/mate

Once done, you can call mate and pass it any object you want to open, such as:

$ mate my_rails_app

and it opens the entire directory as if it were a TextMate project.

Lots more detail available here: http://manual.macromates.com/en/using_textmate_from_terminal.html

Using Cucumber, Webrat and Selenium to test ajax form field validations

September 4th, 2009 by andy 2 comments »

I have an app that fires an ajax request on a form field to validate its contents when I take focus off the field.

I am also using Cucumber, Webrat and Selenium for my integration tests.

I needed my integration tests to test the Ajax responses and the tests weren’t receiving a response from the Ajax requests.

The Problem

I found that by simply completing the web form, the ajax request was not being fired and my test was therefore failing when i checked for the existance of the Ajax response. It soon became clear that selenium doesn’t really interact with the form in the sense of selecting fields and entering values; It simply enters values. As such, the Ajax request was not firing and my test was failing.

The Solution

The solution is Selenium’s fireEvent method, which you can pass a form field id and the blur method:

selenium_session.fireEvent(”field”, “blur”);

In Webrat, this is ever simpler:

fire_event(”field”,”blur”)

On using this in a Cucumber step, the ajax is fired as the blur command tells the browser to take focus off the field.

Lovely!