SEO Optimization Using Rails, Rack, and 301 Redirects

Date Published:
Last Modified: by

I have been performing SEO optimization here at Spazstik Software. Up until recently I have had multiple sites that represented my web presence. My main site is I also have a blog that had two domains:, and A shortcut domain for my main site And finally I have the domain/landing page for our StackCalc iPhone application All of these domains also have the variance of the 'www' or no 'www'.

I use for all of these domains. The main is hosted on Amazon Web Services and I was using Amazon's Route 53 to perform the domain service for only the site.

All other domains were using's domain forwarding feature. This service only performs a 302 redirect. The short of this is that the 302 redirects caused canonical errors when I transitioned to my current web site and in turn was hindering my search engine cheddar.

If you review the http status codes rfc the 302 http response code is labelled as "Found." The description for this response is that the requested resource is temporarily found under a different URI. Which you can imagine will cause google's search engine to not perform any extensive updates. The recommended process for moving your content is to issue a 301 redirect. This indicates that the content has permanently moved over to a new URI. This is what I needed to do. unfortunately only supports domain forwarding using 302 redirects. This required me to come up with a different solution. The solution that I settled on was to move the domain services over to AWS Route 53, route all domains to my web server, and configure the server to issue the 301 redirects.

My web site is a Ruby on Rails application deployed on AWS Elastic Beanstalk. Rails offers a couple of solutions. One solution is to create a before filter in the application controller file and use the standard redirect to statement to perform the redirects. However, the better solution is to configure the rack middleware stack to perform the redirects at a low level. This offers the advantage of requiring less processing on the server then the first solution.

If you are not familiar with Rack I recommend checking out the podcasts, listed below, at the RailsCasts. Ryan Bates has done a excellent walkthrough of the rack middleware stack. Just be aware that some of the episodes require a subscription (well worth the price).

I decided to use the rack-rewrite gem. This gem provides a Rack::Rewrite class that can be inserted into your applications middleware stack along with helper methods for performing your redirects or rewrites.

<script src=""&gt;&lt;/script>

You can use this gem in a couple of ways. You could add it to your file, or from your rails application. I chose the latter. This is shown above. To do this you need to call config.middleware.insert_before. The example shows that it is inserting the Rack::Rewrite object into the middleware stack before the Rack::Lock layer.

In a production deployment the rack middleware starts with a Rack::Cache layer followed by the Rack::Lock layer. In a development deployment the rack middleware starts with an ActionDispatch::Static layer followed by the Rack::Lock layer. So inserting before the Rack:Lock layer is consistent between both environments.

The first set of r301 rules force redirects to the specific landing page for the StackCalc application, both with and without the 'www'. The second set of r301 rules force redirects from the spazsoft domains to domain. These include passing the path to the new site so to redirected to

I use Pow in my development environment to test this configuration. I have a link in the .pow directory for each domain that I need to support (I use the same rails project directory for each domain). The only difference is that I need to use .dev as opposed to .com in my urls.

One thing to avoid is using page caching. Page caching happens at the web server level (a lower level). For example, at the Apache/nginx level. The web servers will intercept the request before the redirects if it finds a static copy of your page in your application public directory and respond with that file, but with the wrong domain. If you need web caching, you will probably need to configure rewrites in your web server configuration files.

In my case where I am using AWS Elastic Beanstalk which uses the nginx web server, this option is not available. Using the rails expire caching methods are still available to you. So In my opinion you lose very little.


  2. Amazon Web Services
  3. Amazon Route 53
  4. http status codes rfc
  5. Ruby on Rails
  6. RailsCasts Episode #319 Rails Middleware Walkthrough subscription required.
  7. RailsCasts Episode #317 Rack App from Scratch subscription required.
  8. RailsCasts Episode #151 Rack Middleware
  9. Rack: a Ruby Webserver Interface
  10. Pow: A Zero Config Rack Server
  11. rack-rewrite
  12. AWS Elastic Beanstalk

Recent Articles

A Reusable Observer Protocol Written In Swift

One design pattern that I use a lot is the observer pattern. The observer pattern is used when you have an object that needs to notify a list of objects that state changes have happened. This article discusses a reusable component, in Swift, I developed to speed up my development process.

How To: Support User Editable Python Macros In A I Os Application

Last month I published a article on how to use JavascriptCore for extending a iOS application with macro support. While Javascript has many uses, as a way for application customization, it would not be my first choice.

A better choice to me would be a language like Python. Being curious, I wondered what it would take to to use Python. This article discusses what I found.

How To Example: Extend A I Os Using Javascript Core As A Macro Engine

JavascriptCore is a framework that offers the ability for a iOS application to interact with javascript code. Primarily used for cross platform code sharing, it can also be used to extend a iOS application with macro capabilities. This framework offers a world of possibilities for extending any iOS application.

How To: Custom I Os Activity Tracker View Using Ca Layers

The Apple Watch shipped with a captivating activity tracker. The center piece is a really cool spiral animation scheme showing the amount of activity during the day. This image is also shown on the matching iPhone Activity App. I have always wanted to see what it would take to implement this myself. The examples that I see typically use a custom drawRect override, but I always wanted to see what it would take to do with CAShapeLayers.

Implementing a 0-100% control is straight forward when using CAShapeLayer. But how do you implement a progress indicator that support progress values greater then 100%? This How To discusses a solution that I came up with along with it’s potential limitations.

How To Display Custom Content On A External Screen From A I Os Device

Being able to display content on a external screen or device is a great capability to add to a iOS application. Especially how easy it is. This article will show the step required to to do this.


Follow us on

Articles by published month

Articles by subject matter

Rails Thor Compass Susy Modernizr Rspec Capybara Bettererrors Railspanel Aws Rack Railscasts Http Aws-elastic-beanstalk Ruby-on-rails Rack-rewrite Http-response-codes Pow-amazon-route-53 Stackcalc Iphone Ios Mobile Application Skeumorphic Dns Web-site Elastic-beanstalk Elastic-ip Elastic-load-balancer Tutorial Howto Javascript Javascriptcore Macro Example Swift Design-patterns Observer Python Macros Alamofire External-screen-support Apple-watch Activity-tracker Office Status S100 Cloud Astronomy Picture Mars Apple Usb-c Leonard-nimoy William-shatner Geotag Gps Spztracker Geotagging Photos Secret Marketing Watch Watch-repair Head-transplants Perception Diabeties Sugar Health Rosette-nebula News Spock Comet-lovejoy

Click here to receive free tips and tutorials!

This web site uses javascript exclusively for automating html elements. Please enable javascript to fully experience the features offered on this site.