How to remove a URL's trailing slash in a Rails app? (in a SEO view)
I'd use Apache's mod_rewrite. Try this:
RewriteEngine on
RewriteRule ^(.+)/$ $1 [R=301,L]
EDIT: Added R=301. I'm guessing there is an SEO advantage to that vs. the default 302. Trailing Slash Behavior in Rails Application
I'm not sure there isn't something in rails that does it for you, but this should do:
class TrailingSlashes
def initialize(app)
@app = app
end
def call(env)
if match = env['REQUEST_PATH'].match(/(.*)\/$/)
response = Rack::Response.new
response.redirect(match[1])
response
else
@app.call(env)
end
end
end
Rails 2.3 - How to make all URL's end with a forward slash / character?
While I agree with Gareth's sentiment, there is some truth to what he says; Google will treat URLs that are different in any way as ... different. Let's say 10 sites have linked to /home/index and another to /home/index/ -- all this inbound linking Google Goodness becomes dispersed. So, there are a few things to do, all falling under the headline of "URL Canonicalization".
Note: your title says Rails 2.3 (Some of the details below may apply only to Rails 3.x).
First, here's the answer to your question: How to generate links with trailing slash in Rails 3?
But there's more, and maybe if you blow away your SEO consultant with your incredible knowledge, he'll crawl back to the slimey ooze to be amongst his kind. (Joking, I do SEO for a living -- I much prefer the zombie filled swamp).
First and foremost, all pages having the same content should have the one and only one URL. Your code needs to generate that URL in link_to
, and url_for
calls, which generally speaking Rails does out of the box.
Second, and almost as important, while multiple URLs may lead to a given page, you should choose one and ensure that the others are "Permanently Redirected" to the canonical URL. The most common case of this is that sites typically will respond to "www.example.com" and "example.com", and since this is part of your URL, you should pick one or the other, and make sure that's where it lands. The traditional way to do this is to use RewriteRules in Apache (and I think Nginx), but if you're not familiar with them, they will want to make you decide software was a bad career path, and that one of those Dirty Jobs would be preferable. Here's a gem that does it for you: https://github.com/tylerhunt/rack-canonical-host
Finally, it's a good idea to specify the canonical URL in the page itself -- there's a special link tag format documented here: http://googlewebmastercentral.blogspot.com/2009/02/specify-your-canonical.html. The case where this tends to matter is when you're adding query-string parameters (e.g. sorting, filtering, searching, paginating, or even just for tracking) that look different to Google, but are pretty much the same page. It's not terribly hard to do, but here's a gem from a while back that you can look at if you want https://github.com/mbleigh/canonical-url
A final tip: generate a sitemap and submit it to Google and other search engines. I have used this gem and it works really well: https://github.com/kjvarga/sitemap_generator
How to generate links with trailing slash in Rails 3?
You can add this to your application.rb:
config.action_controller.default_url_options = { :trailing_slash => true }
This way all routes will be generated with a trailing slash automatically, with no need to modify each link in your project. How to make a page accessible by two urls for seo purposes?
If both the routes are performing the same task then route them to the same controller#action
in config/routes.rb
.
For example:
get 'tires', to: 'welcome#index'
get 'lawn-and-garden', to: 'welcome#index'
UPDATE:If I understand you right, then you would like the page to be accessible by both routes /tires?product_subtype=1
as well as /industrial-tires
(without query param). We have done something similar on one of our projects, we call these pretty url's as landing pages. I can think of two options to implement these landing pages:
If you have a fixed number of very few landing pages:
create an action for each of them which renders corresponding subtype view.
def industrial_tires
## render view filtered for product_subtype = 1
end
def commercial_tires
## render view filtered for product_subtype = 2
end
## .... so onIf you have many/ variable number of landing pages:
you will have to create a low priority catch all route and within the mapped action conditionally render specific view based on the slug.
get '*path', to: 'tires#landing_page' ## in routes.rb at the end of the file
def landing_page
## "path" would be equal to industrial-tires or commercial-tires, etc.
## conditionally specify view filtered for product_subtype based on path value
end
slashes in url variables
You need to escape the slashes as %2F
.
Related Topics
How Can Multiple Trailing Slashes Can Be Removed from a Url in Ruby
Deleting a Specific Line in a Text File
Ruby Require 'File' Doesn't Work But Require './File' Does. Why
Ruby Fails on Osx Lion with Rbenv
Running "Bundle Install" Fails and Asks Me to Run "Bundle Install"
Multi Step Form with Image Uploader
How to Implement Curry(Partial Function) in Ruby
Rendering a View from My Ruby on Rails Gem
Segmentation Fault with Ruby 1.8.7
Where to Reopen a Class in Ror
Heroku Repo Size and Slug Size Increase with Each Deployment. Why
How to Directly Install a Gem from a Git Repository
Sinatra Not Persisting Session with Redirect on Chrome
Is There a Command Line Framework for Ruby