NoobOnRails

Ruby on Rails tips, tricks and hints for the aspiring rails hero.



Wednesday, September 27, 2006

Good SEO mojo with Rails


So you've created your site that allows you to sell all the cool desinger knitted sweaters you've made over the past few weeks and you've just stuck your site online. Five months later, no one has visited your site outside of you and grandma and she didn't even buy anything. Cheapskate. At this point you realize, to make it big, you have to get some decent ranking on the big search engines.

Now, I'm not going to even pretend like I'm MR. SEO or anything but I'm going to post a few tips that I believe can help, not harm, your page ranking and hopefully give you somewhere to start from. I'll start with three tips, if you can think of anything else, please, feel free to comment below. If you have any requests, put'em up. I can't guarantee I can fulfill them but at least there's a chance it'll spark some interest or conversation. So, here are three basic Ruby on Rails SEO tips:


1. WWW or no WWW

Without any configuration on your server or routes, your website can be accessed two ways, http://yoursite.com and http://www.yoursite.com. Seems harmless but the problem with this is, or so I've heard, that some search engines may consider them as two seperate sites and think that you're duplicating your content, as a means to try and trick them. They tend to frown on this so the quicker you can avoid this, the better.
UPDATE: As the anonymous posted below pointed out, the matt cutts article I had in mind when writing this, doesn't necessarily take this stance. He states that you should basically use redirects as a means to point the search engine's crawlers to the right content. I could've sworn I heard the double content idea from somebody though. Oh well, false alarm.

I believe the best and most effective way to handle this is at the web server level and for me, I'm currently using Mongrel to host my cookmor site (no www's in that link, click it and watch it redirect) so this is how I did this redirect for Mongrel: You first need to create a mongrel.conf file to throw this code in (you'll call it when you start Mongrel, you'll see later) so create a mongrel.conf file and stick it in your railsapp/log/ folder. This goes in the mongrel.conf file:

class RedirectHost <>

def process(request, response)
if request.params["HTTP_X_FORWARDED_HOST"] == "yoursite.com"
response.start(302,true) do head, body
head["Location"] = "http://www.yoursite.com/"
head["Content-type"] = "text/html"
body.write("Please go here instead: http://noobonrails.blogspot.com/")

end
end
end

end

uri "/", :handler => RedirectHost.new, :in_front => true


Once that file is created and saved, stop mongrel. Restart it by using this statement:

mongrel_rails start -e production -p YOURDESIREDPORT -d -S log/mongrel.conf

Booyah. Now chances are you may not be using only mongrel to host your site, and that's ok, so to compensate, here's how you do it with lighttpd, here's how you do it with Apache and even how to do it with LightSpeed. That's all I could think of.


2. Prettier URLs

This really depends on how you define pretty. If you define pretty as http://www.yourbooks.com/books/show/rails-is-fer-heroes, then you're ok. If not, you're will be getting nothing but tumbleweeds my friend. The whole thinking here is that, if the urls are easier for the search engines to read and understand, it will be easier for them to match up keywords to the proper pages on your site. Just don't try to fit too many keywords in your url, I'm sure /show/credit_reporting_for_credit_reporters_reporting_on_credit will look a little suspicious.

So, say you have a book website on which you list your books. You also have a show action in your controller that is called whenever you want to show the details of a certain book. Currently, the url on your show book page looks something like this, http://www.mybooksrox.com/books/show/422. Now that's all fine and dandy but we can do better for Google. Let's create a "named route" in your config/routes.rb file (more on routes here). So above the

map.connect ':controller/:action/:id'


that is the defacto standard route that all routes.rb contain at the onset, stick this route:

map.book 'books/:booktitle', :controller => 'books', :action => 'show'

What this is doing is telling rails to generate a URL of books/whateverbooktitleis when invoked from the books controller and show action. Where do we define booktitle? Where ever we make a call to show a recipe, like this:

<% for book in @books%><li>* %lt;%= link_to book.title, book_url(:booktitle => book.title) %></li>

<% end -%>
This does assume that you have a title column in your books table. book_url is actually calling the named route we created and we're passing the booktitle param along with it. Save your files and on whatever page you created those links on, they should now point to a url which includes the book titles.

3. Google Sitemaps

Now that I've typed this much, I can't really rememebr what number 3 was supposed to be... oh well. The next tip I could think of is to use Google Sitemaps for your site so that Google can easily serach and index it. There's a good, basic article here that seems just fine. For mre information on Google sitemaps and why they're the bees kness, check out their site here.

That's all I could think of for now. If I missed anything, please let me know. Happy coding.

5 Comments:

  • At 1:21 PM, Anonymous Anonymous said…

    Item number 1 is the stupidest thing I've ever heard of. If a search engine treats www.domain and domain as duplicate content, then the search engine has problems.

     
  • At 4:17 PM, Blogger Curtis said…

    I agree with it being a bit odd but apparently it's how Google works. Google considers http://domain.com and http://www.domain.com different addresses, here's some more information: http://www.mattcutts.com/blog/seo-advice-url-canonicalization/

     
  • At 4:18 PM, Blogger Curtis said…

    sorry, try clicking here

     
  • At 7:35 PM, Anonymous Anonymous said…

    You need to re-read Matt's post. What it's talking about is that Google will pick the best representation of your site from the different URLs. It does not say it will hurt your placement or de-list you.

     
  • At 9:19 PM, Blogger Curtis said…

    You're right. The cutts post I pointed too isn't nearly as doom and gloom as mine was. I'll stick an update in my post.

     

Post a Comment

<< Home