English 中文(简体)
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.

最佳回答
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
问题回答

This method is the easiest and with the added bonus of sending a 301 header. This is really good to improve your SEO rating!!!

<?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");
}
?>

Make sure to have this code at the very beginning of your script or bootstrap

With this method, you not only redirect your audience and at the same time, you re notifying google (or whatever search engine) of the change. This will make google update it s index in no time.





相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签