【问题标题】:Needs redirects for routing需要重定向路由
【发布时间】:2018-04-13 17:04:29
【问题描述】:

我对用于 Rails 5 的 Camaleon cms 感到非常兴奋;但是,我注意到在 url 中有父 slug 或 post-type slug 作为 url 格式的帖子存在一个重大问题。

对于背景,帖子的内容只能通过一个 URL 访问,这一点非常重要。否则,您有可能因重复内容而在谷歌中受到处罚。对于那些依赖搜索引擎流量的人(基本上所有使用过 CMS 的人)来说,这是一个非常严重的问题。

以下是该问题的示例。所有这些 url 都将呈现相同的帖子内容:

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 处理此问题的方式是,如果父 slug 不存在或拼写错误,则使用正确的父 slug 重定向到正确的 url。

我的问题是针对那些知情人士,这可能是即将发布的版本之一中的优先问题吗?

【问题讨论】:

    标签: ruby-on-rails-5 camaleon


    【解决方案1】:

    我不确定这是否适用于所有人,但这是我对这个问题的解决方案。

    要求:这仅适用于具有“post_of_posttype”、“heirarchy_post”或“post_of_category_post_type”路由格式的帖子。

    以下代码通过在参数与@post.the_path 不匹配时简单地添加重定向来扩展 Camaleon 的前端控制器方法 render_post 的功能。似乎为我的目的工作。希望它会帮助别人。

    在您的 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 的人正在创建面向内部的应用程序。无论如何,如果不是这样,我认为这应该是优先解决的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-19
      • 1970-01-01
      • 1970-01-01
      • 2018-10-15
      • 2021-07-20
      • 1970-01-01
      • 2021-09-09
      • 2013-11-22
      相关资源
      最近更新 更多