How to Generate Links with Trailing Slash in Rails 3

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 add a trailing_slash to all urls without in Rails 4?

I think you have the meaning of :trailing_slash => true wrong.

All it does is add the / to the end of you path helpers. No redirecting involved.

Your routes will still respond to both with and without the trailing slash.

If you want to redirect all non-trailing_slash uri's like /download to /download/ using a nginx http server you would do something like this:

rewrite ^([^.\?]*[^/])$ $1/ permanent;

You would still want to add the :trailing_slash => true to your routes so your path/url helpers generate the the correct uri's (so user don’t need to redirect).

How to make Rails do not ignore trailing slashes in the routes?

I'm not sure why you need your routes to understand that there is a trailing slash. It doesn't matter if you put the trailing slash to Rails. It should respond either way.

If you want all of your links to have a trailing slash, you can put the following in your application.rb:

config.action_controller.default_url_options = { :trailing_slash => true }

This will make all links generated by Rails have a trailing slash. Now, if you're trying to reject any route that does not contain a trailing slash, then I'm not sure about how to do that.

Matching URLs with a trailing slash in Rails' routes.rb

One solution I've found is to use request.env["REQUEST_URI"], which contains the raw URL submitted with the request. Unfortunately, since it's not a direct string property of the request, it requires a custom matching object:

class TrailingSlashMatcher
def matches?(request)
uri = request.env["REQUEST_URI"]
!!uri && uri.end_with?("/")
end
end

AppName::Application.routes.draw do
match '/example/*path', constraints: TrailingSlashMatcher.new, to: redirect("/somewhere/")
end

That seems like overkill, so hopefully someone has a more elegant approach.

Enforce trailing slash in Rails Routing

in the config/application.rb file, add

config.action_controller.default_url_options = { :trailing_slash => true }

in the Application class

Clean URLs Using forward slash '/' in to_param with Rails (3)

This is indeed an old post, but I want to build a bit on it.

If you don't want to have to handle an slug variable in your params, you really need to define that method to_param in your model


def to_param
"#{id}/#{title}"
end

and set a route like so:

resources :posts, :id => /[0-9]+\/.+/

That way your link definition looks quite like a normal one:

link_to post.title, post_url(post)

Very simple: http://www.miguelsanmiguel.com/2011/03/17/slug-that-slash

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



Related Topics



Leave a reply



Submit