Search code examples
ruby-on-railsroutesrails-routing

How can I redirect requests from Rails to a PHP subdomain?


I have a site, say, example.com, which is PHP. I am converting it to Rails, but there are three years worth of issues (like magazine issues) that I don't want to update. Thankfully, it seems that I chose an advantageous url format, ie. all issues start with two digits, then the name of the file in most cases

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

I want to redirect via 301 all requests for those php files to

archive.example.com

And make the top level example.com a Rails site, serving up the latest issues.. ~/20/author-name

The subdomain is on dreamhost, and the top level will go to heroku. (So this is not part of the question.) Thanks.


Solution

  • 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
    

    For any request coming to domain/00 to domain/19 I redirect in the controller

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