如何在 Rails 应用程序中删除 URL 的尾部斜杠?(在 SEO 视图中) [英] How to remove a URL's trailing slash in a Rails app? (in a SEO view)
问题描述
为了避免内容重复,我想避免我网站的页面被多个 URL 访问(带或不带斜杠).
In order to avoid content duplication, I would like to avoid the pages of my site being accessible by several URLs (with or without trailing slash).
目前,网址
catalog/product/1
和
catalog/product/1/
引导到同一页面.我的目标是第二个 URL 重定向到第一个(当然是重定向 301).除了我的主页/显然,我网站的任何页面都不应该用斜杠访问.
lead to the same page. My goal is that the second URL redirect to the first (redirection 301, of course). None page of my site should be accessible with a trailing slash, except my home page / obviously.
这样做的最佳方法是什么?使用 .htaccess 还是 routes.rb?你会怎么做?
What is the best way to do this? Using .htaccess or routes.rb? How would you do that?
注意:我正在使用 Ruby on Rails 1.2.3 进行开发
NB: I'm developing with Ruby on Rails 1.2.3
推荐答案
我会使用 Apache 的 mod_rewrite.试试这个:
I'd use Apache's mod_rewrite. Try this:
RewriteEngine on
RewriteRule ^(.+)/$ $1 [R=301,L]
添加了 R=301.我猜这与默认的 302 相比具有 SEO 优势.
Added R=301. I'm guessing there is an SEO advantage to that vs. the default 302.
这篇关于如何在 Rails 应用程序中删除 URL 的尾部斜杠?(在 SEO 视图中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!