Short URLs With Webby and htaccess

Here’s a quick way to create short URLs for any page on your Webby-created website. You can even use this to create short URLs to external websites, so you can have your own personal shortening service.

We are going to create a dynamic .htaccess file using Webby. If you already have a content/.htaccess file, rename it to content/htaccess.txt or similar. We need to add some Webby metadata to the top of the page.

layout: nil
dirty: true
  - erb

layout:nil tells Webby not to use a HTML template for this file. dirty: true means the file will be regenerated with every build. Finally filter: erb tells Webby to process any erb statements and put the output into our document.

Now, we are going to manually add a shortcut: x item to the metadata of any page we’d like to create a shortcut to, and then create a redirect line for each shortcut in the htaccess file.

<% codes_used = [] %>

<%  @pages.find(
    :in_directory => @page.dir, 
    :recursive => true, 
    :sort_by => :shortcut, 
    :reverse => true
  ).each do |p| -%>
<% break if p.shortcut.nil? -%>
<% codes_used << p.shortcut -%>
<%= "Redirect 301 /s/#{p.shortcut}{p.url}" %>
<% end -%>

This will create lines in the htaccess file like so:

Redirect 301 /s/1

You could omit the /s/ if you wanted, and have links like This would be shorter, but it would clutter up the namespace at the root of your URLs. Decide which is more important to you.

To link to external pages, I have just created an array of 2-element arrays called SHORTCUTS in my Sitefile, and then create links like so:

<% SHORTCUTS.each do |s, u| -%>
<%= "Redirect 301 /s/#{s} #{u}" %>
<% codes_used << s -%>
<% end -%>

You may have noticed that I’ve been adding the shortcuts to an array called codes_used, I use this for some very crude duplicate detection to make sure I haven’t defined the same shortcut twice.

<% raise "duplicate codes in " + codes_used.sort.join(", ") unless codes_used.uniq.length == codes_used.length -%>

That’s all the dynamic content, but you can use the rest of the file for any other htaccess config.

# Put other htaccess config below.

Options -Indexes 

You can download the full script. (You should remove the ### comments from this file if you use it.)

If you have followed my naming convention then you’ll end up with a file called htaccess.txt in the output directory. We need this to be called .htaccess instead. Because of the way Webby automatically creates filenames, it’s easiest to just rename this file ourselves in a Rake task. In my Sitefile I define a task:

desc "rename htaccess.txt to .htaccess"
task :htaccess do
  `mv output/htaccess.txt output/.htaccess`

and I have my deploy task call this task, so it happens automatically.

desc 'deploy the site to the webserver'
task :deploy => [:build, :htaccess, 'deploy:rsync']

If you’re interested in the analytics of your shortcuts, you’ll be able to parse your access logs (try splunk). You might want to have some easy way to distinguish internal from external links, you could do this by having external shortcuts use a different path such as I don’t think that Javascript-based analytics (like Google Analytics) will detect that this redirect has taken place.

Source Code