Chr*_*fer 12 redirect ruby-on-rails heroku
大约一年前,我将三个网站(oldsite.com,oldsite.nu,newsite.se)合并为一个,我保留在其中一个域(newsite.se)上.我不确定这是否已经完成,因为即使在一年之后,我仍然会看到来自谷歌的旧网址的大量流量.
Oldsite重定向代码
重要的编辑说明:我最近意识到名称服务器不再指向我的旧rails应用程序,而是指向我的Web主机上的php文件夹,其中我有一个.htaccess,代码如下:
RewriteEngine on
RewriteRule ^robots.txt - [L]
RewriteRule ^sitemap.xml - [L]
RewriteRule ^(.*)$ http://www.newsite.se/$1 [R=301,L]
Run Code Online (Sandbox Code Playgroud)
这使得下面的这一部分(关于oldsite.com/oldsite.nu)无效: .com和.nu是在Ruby on Rails中构建的,并在Heroku上托管.
从oldsite.com/oldsite.nu 重定向路径的逻辑完全在newsite.se站点上完成.oldsites上的重定向代码是一个简单的重定向,在oldsite.com上的routes.rb的第一行是这样的:
Run Code Online (Sandbox Code Playgroud)match "/(*path)" => redirect {|params, req| "http://www.newsite.se/#{params[:path]}"}, via: [:get, :post]
我使用这个(瑞典语)工具来验证这个重定向实际上是301重定向:http://301redirect.se.它确认重定向为301.
Newsite.se重定向处理程序
每个旧网站上的内容与新网站上的相同内容相匹配,很少在相同的路径上,例如
oldsite.com/categories/vacation/item/1243
Run Code Online (Sandbox Code Playgroud)
可能会导致
newsite.se/product-items/1243
Run Code Online (Sandbox Code Playgroud)
我主要在内部重定向控制器中处理这些类型的重定向,该控制器捕获并重定向newsite.se上的任何流量,如:
newsite.se/categories/vacation/item/1243 -> newsite.se/product-items/1243
Run Code Online (Sandbox Code Playgroud)
在我的newsite.se routes.rb底部使用它:
match '*not_found_path', :to => 'redirections#not_found_catcher', via: :get, as: :redirect_catcher, :constraints => lambda{|req| req.path !~ /\.(png|gif|jpg|txt|js|css)$/ }
Run Code Online (Sandbox Code Playgroud)
这很好用.
编辑20151223:我使用Newsite.se处理重定向的原因是因为它包含重定向路径的所有逻辑.Oldsite.com/.nu几乎不可能知道这一点.
所采取的行动
在重定向301之外(据我所知,我这样做).我还使用Google网站管理员工具从旧的两个网站向我的新网站发出"请求更改地址".我再也找不到任何关于此的信息了,但我确信我得到了WMT的积极回应,这个游戏已经完成(但我不是100%肯定).
问题的迹象
我不是100%确定有什么问题,但我已经看到让我相信重定向不正确的迹象,以便Google真正意识到网站没有被移动.
解决问题
我必须为移动大型电子商务网站的客户执行类似的操作,该网站需要将所有旧流量转移到新网站并将适当的产品重定向到新路径。
为了让一切都进行转换,这样我们就不会失去 Google 排名,我们必须实施 301 重定向,正如您上面提到的。在 WMT 中,他们似乎依赖您来处理这个问题,而不是再将其作为受支持的功能。
方法
您应该将旧域上的每个 URL 重定向到相应的新 URL。这是根据 Google 记录并推荐的更改域的方法。
最好的方法是在控制器中处理重定向,并使用逻辑将其发送到带有 301 的实际页面,并且一旦登陆新网站就不再进行重定向。
我建议如下:
路线.rb (oldsite.com/oldsite.nu)
匹配请求并将其发送到控制器以处理更精细的逻辑和 301。
match "/(*path)", to: 'redirector#catch_all', via: [:get, :post]
Run Code Online (Sandbox Code Playgroud)
RedirectorController (oldsite.com/oldsite.nu)
def catch_all
# Separate the rest of the link into its components
# I will use the example of the vacation -> product-items you have
options = params[:path].split('/')
options.reject! { |e| e.to_s.empty? } # Remove trailing junk if any
if options[0] == 'categories'
redirect_to "http://www.newsite.se/product-items/#{options.last}", notice: 'Product Updated! We Have A New Site.', status: 301
return # the return here is a MUST for more complex if-then and multiple redirect_to's
elsif options[0] == 'contact'
redirect_to "http://www.newsite.se/contact-us", notice: 'We moved, Contact us Here.', status: 301
return
elsif options.empty? || options.blank?
redirect_to "http://www.newsite.se/", notice: 'That page no longer exists, Please browse our new site', status: 301
return
else
more_sorting(options)
end
end
private
def more_sorting(options)
case options
when options[2].....
redirect_to ....., notice: '', status: 301
return
when options[3].....
redirect_to ....., notice: '', status: 301
return
when options[4].....
redirect_to ....., notice: '', status: 301
return
end
end
Run Code Online (Sandbox Code Playgroud)
为什么要这样做:
这将导致搜索引擎机器人和用户仍然能够抓取和访问每个页面和链接,并重定向到新网站上与其关联的特定页面。
此外,它还处理该服务器上的 301 重定向,并且不会导致新服务器上的另一个重定向。您可能会因为用户体验和机器人对您尝试联合站点的解释而受到惩罚。(这也很可能会删除链接 301 的解释)
如果您需要更复杂的路由,您可以在 RedirectController 中添加(就像我必须做的那样)私有函数,以便更深入地分析我在elseif then中最后一个参数。
澄清?
如果您还有任何其他问题以及这是否有帮助,请告诉我。
| 归档时间: |
|
| 查看次数: |
174 次 |
| 最近记录: |