【问题标题】:How can I redirect requests from Rails to a PHP subdomain?如何将请求从 Rails 重定向到 PHP 子域?
【发布时间】:2011-01-07 05:49:15
【问题描述】:

我有一个网站,例如 example.com,它是 PHP。我正在将其转换为 Rails,但有三年的问题(如杂志问题)我不想更新。值得庆幸的是,我似乎选择了一种有利的 url 格式,即。所有问题都以两位数字开头,然后在大多数情况下是文件名

example.com/00/author-name/index.php
example.com/19/author-name.php

我想通过 301 将所有对这些 php 文件的请求重定向到

archive.example.com

让顶级 example.com 成为 Rails 站点,提供最新问题.. ~/20/author-name

子域在dreamhost上,顶层将转到heroku。 (所以这不是问题的一部分。)谢谢。

【问题讨论】:

    标签: ruby-on-rails routing rails-routing


    【解决方案1】:

    【讨论】:

    • 我将不得不问 heroku 我是否可以修补他们的应用服务器,瘦。我更喜欢制作路线。
    【解决方案2】:
    ActionController::Routing::Routes.draw do |map|
    
      map.connect '20/:name', :controller => :twenty, :action => :show
      map.resources :twenty, :as => '20', :only => [:index, :show] 
    
      map.connect ':url', :controller => :archive, :action => :show,
                         :requirements => { :url => /(([0-1]){1}([0-9]){1})(.*)/ }
    
      map.root :controller => :pages, :action => :cover  
    
      map.connect ':controller/:action/:id'
      map.connect ':controller/:action/:id.:format'
    end
    

    对于任何来自 domain/00 到 domain/19 的请求,我都会在控制器中重定向

    redirect_to "http://archive.example.com/#{params[:url]}", :status => 301
    

    【讨论】:

      【解决方案3】:

      这种方法是最简单的,而且还有发送 301 标头的额外好处。这对提高您的 SEO 评级非常有用!!!

      <?php 
      $uri = $_SERVER['REQUEST_URI']; // Gets the user's current URI
      $redirect = array("/00/author-name/index.php", "/19/author-name.php"); //Define your 301 redirect uri
      
      // Here's the meet and greet of your problem:
      if (in_array($uri, $redirect)) {
          header("HTTP/1.1 301 Moved Permanently");
          header("Location: archive.example.com");
      }
      ?>
      

      确保在脚本或引导程序的最开头包含此代码

      使用这种方法,您不仅可以重定向您的受众,同时还可以将更改通知谷歌(或任何搜索引擎)。这将使谷歌立即更新它的索引。

      【讨论】:

        猜你喜欢
        • 2011-07-24
        • 2020-04-24
        • 1970-01-01
        • 2012-07-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多