How can I redirect requests from Rails to a PHP subdomain?
-
21-09-2019 - |
Question
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 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
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
OTHER TIPS
Have a look at the following answers:
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.