我对Camaleon cms for rails 5感到非常兴奋;但是,我注意到有一些帖子存在重大问题,这些帖子有父句或者网址中有post-type slug作为url格式。
对于背景知识,帖子的内容只能通过一个网址到达,这一点非常重要。否则,您可能会因谷歌内容重复而受到惩罚。对于那些依赖搜索引擎流量的人(基本上每个人都会使用CMS),这是一个非常严重的问题。
以下是该问题的示例。所有这些网址都会呈现相同的帖子内容:
http://www.example.com/parent_slug/post_slug
http://www.example.com/post_slug
http://www.example.com/parent_slug_blah_blah/post_slug
或者
http://www.example.com/post_type/post_slug
http://www.example.com/post_slug
http://www.example.com/post_type_blah_blah/post_slug
Wordpress处理此问题的方法是使用正确的子段重定向到正确的url(如果它不存在或者拼写错误。)
我的问题是针对那些知情人士,这可能是即将发布的版本中的一个优先问题吗?
答案 0 :(得分:0)
我不确定这是否适用于所有人,但这是我解决此问题的方法。
要求:此功能仅适用于包含" post_of_posttype"," heirarchy_post"或" post_of_category_post_type"路线格式。
以下代码扩展了Camaleon的前端控制器方法render_post的功能,只需在params不匹配@ post.the_path时添加重定向。似乎为我的目的工作。希望它会帮助别人。
在config / initializers文件夹中创建一个新文件,并放置以下代码:
# config/initializers/camaleon_custom_post.rb
CamaleonCms::FrontendController.class_eval do
# render a post
# post_or_slug_or_id: slug_post | id post | post object
# from_url: true/false => true (true, permit eval hooks "on_render_post")
def render_post(post_or_slug_or_id, from_url = false, status = nil)
if post_or_slug_or_id.is_a?(String) # slug
@post = current_site.the_posts.find_by_slug(post_or_slug_or_id)
elsif post_or_slug_or_id.is_a?(Integer) # id
@post = current_site.the_posts.where(id: post_or_slug_or_id).first
else # model
@post = post_or_slug_or_id
end
unless @post.present?
if params[:format] == 'html' || !params[:format].present?
page_not_found()
else
head 404
end
else
@post = @post.decorate
if ["post_of_posttype","hierarchy_post"].include? @post.the_post_type.contents_route_format
if params[:parent_title].nil? && params[:post_type_title].nil?
params_path = "/" + params[:slug]
elsif !params[:parent_title].nil?
params_path = "/" + params[:parent_title] + "/" + params[:slug]
elsif !params[:post_type_title].nil?
params_path = "/" + params[:post_type_title] + "/" + params[:slug]
end
unless (@post.the_path === params_path)
redirect_to @post.the_url, status: 301 and return
end
elsif @post.the_post_type.contents_route_format === "post_of_category_post_type"
if [params[:post_type_title],params[:label_cat],params[:category_id],params[:title]].all?
params_path = [params[:post_type_title],params[:label_cat],params[:category_id] + "-" + params[:title],params[:slug]].join("/")
params_path.prepend("/")
unless (@post.the_path === params_path)
redirect_to @post.the_url, status: 301 and return
end
else
redirect_to @post.the_url, status: 301 and return
end
end
@object = @post
@cama_visited_post = @post
@post_type = @post.the_post_type
@comments = @post.the_comments
@categories = @post.the_categories
@post.increment_visits!
# todo: can_visit? if not redirect home page
home_page = @_site_options[:home_page] rescue nil
if lookup_context.template_exists?("page_#{@post.id}")
r_file = "page_#{@post.id}"
elsif @post.get_template(@post_type).present? && lookup_context.template_exists?(@post.get_template(@post_type))
r_file = @post.get_template(@post_type)
elsif home_page.present? && @post.id.to_s == home_page
r_file = "index"
elsif lookup_context.template_exists?("post_types/#{@post_type.the_slug}/single")
r_file = "post_types/#{@post_type.the_slug}/single"
elsif lookup_context.template_exists?("#{@post_type.slug}")
r_file = "#{@post_type.slug}"
else
r_file = "single"
end
layout_ = nil
meta_layout = @post.get_layout(@post_type)
layout_ = meta_layout if meta_layout.present? && lookup_context.template_exists?("layouts/#{meta_layout}")
r = {post: @post, post_type: @post_type, layout: layout_, render: r_file}
hooks_run("on_render_post", r) if from_url
if status.present?
render r[:render], (!r[:layout].nil? ? {layout: r[:layout], status: status} : {status: status})
else
render r[:render], (!r[:layout].nil? ? {layout: r[:layout]} : {})
end
end
end
end
似乎有点奇怪,对于非准确网址的强制重定向不在核心camaleon应用程序中,但也许大多数使用此cms的人正在创建面向内部的应用程序。无论如何,如果情况并非如此,我认为这应该是一个优先解决方案。