3

I have switched to Ruby on Rails and my current problem is to reroute the old contents like XXX/dummy.html or XXX/dummy.php in RoR.

What exactly is the best solution for

  1. isolated content (XXX/onlyinstance.html)
  2. content which has a internal structure like XXX/dummy1.html, XXX/dummy2.html

http://guides.rubyonrails.org/routing.html does not explain how to migrate old content.

Note: Changing the old links is NOT an option. The website is hosted, it is not my own server. As the domain hasn't changed, the solution to redirect it seems to be unnecessary...there should be a better solution.

EDIT: I have found out that the best solution is in fact rerouting it by the way weppos described.

So add a .htaccess file in the public directory and write

RewriteEngine on
Redirect permanent /XXX.php http://XYZ/XXX

For whatever reason, RoR did not accept rerouting in routes.rb...while .html/.xml all goes fine, .php does not function. I haven't found out why. Because weppos answer was the best, I will award him a 50 point bounty, but as the others answers are valid, too, I will upvote them. Thank you all

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
Thorsten S.
  • 4,144
  • 27
  • 41
  • What do you mean with reroute? Place the HTML files in your rails application, or converting it to Ruby on Rails to actual replace it? – Veger Jan 14 '10 at 13:03
  • You have an old website written in PHP/(X)HTML/whatever. Now you intend to learn RoR and to do that in practice, you want besides building a new presence convert your old links to RoR. Yes, it is possible to simply place the html files in your RoR directories at the same place (but I find that a bit messy). But if you use *.php endings, my server throws a 500 error and the development.log is empty. – Thorsten S. Jan 14 '10 at 13:46

3 Answers3

4

You can do this in multiple ways.

The best and most efficient way is to use your front end web server. You can easily setup some configurations in order to redirect all the old URLs to the new ones.

With Apache, you can use mod_alias and mod_rewrite.

Redirect /XXX/onlyinstance.html /new/path
RedirectMatch ˆ/XXX/dummy([\d])+\.html$ /new/path/$1

This is the most efficient way both for server and client because handled at server level without the need to initialize the Ruby interpreter.

If you can't/wan't take advantage of server settings, you can decide to use Rails itself. Talking about performance, the most efficient way is to use a Rack middleware which is much more efficient than creating a full controller/action.

class Redirector
  def self.call(env)
    if env["PATH_INFO"] =~ %r{XXX/onlyinstance\.html}
      [301, {"Content-Type" => "text/html", "Location" => "http://host/new/path/"}, "Redirecting"]
    else
      [404, {"Content-Type" => "text/html"}, "Not Found"]
    end
  end
end

There is also a Rack plugin called Redirect that provides a nice DLS for configuring redirects using a Rack middleware.

Just a footnote. I won't creating additional routes using routes.rb because you'll end up duplicating your site URLs and wasting additional memory.

See also Redirect non-www requests to www urls in Rails

Community
  • 1
  • 1
Simone Carletti
  • 173,507
  • 49
  • 363
  • 364
2

What do you mean by migrating? I recommend to redirect clients to use the RoR URLs. This can be done using HTTP 301 status codes. See http://en.wikipedia.org/wiki/HTTP_301:

The HTTP response status code 301 Moved Permanently is used for permanent redirection.

This can be done in the configuration of your HTTP server.

2

You have to redefine your application since Rails uses RESTful routing (as you probably have read). So in order to have a php file which handles show, creating,destroying, etc of items, you need to build a item Model, Controller and views for the different actions.

The static HTML files you can copy to the public directory, since that is the same. The structure you used can still be the same.

In order to modify your routing you have to add map.resource to your config/routes.rb file. This implements the RESTful routes to your controller. To start use the webserver provided by Rails (WEBrick), by entering the script/server command. Later when you have more experience you could think of switching to another server if WEBrick is not sufficient.

I suggest you start writing a basic (blog) application with Rails first, see here. So you see what parts is Rails using and how you can use them.

Afterwards you are able to identify these parts in you PHP solution and are bbetter capable to convert your pages. At least I followed this approach when I started to use/convert to Rails from PHP.

Veger
  • 37,240
  • 11
  • 105
  • 116
  • Links from other sites are referring to "XYZ/xyz.php". Now your intention is that if so. clicks on the link you can reroute the browser to anything even if it isn't PHP. Let's say you want to start automatically XYZ/xyz as RoR app if so. insert "XYZ/xyz.php" in your browser. Is that programmatically possible without access to redirecting with status codes ? Is there sth in RoR (like routing) which is able to do that ? – Thorsten S. Jan 14 '10 at 17:29
  • I have never tried this, but something like this should be possible: `map.resource '/XYZ/xyz.php', :controller => :XYZController, :action => :index`, now RoR takes over over with the index action. From there the user can fully access the application. **But** keeping URLs ending with `.php` even when not using PHP seems weird to me. So at some point you really need/want to migrate to a full-fledged RoR site... Why not start now and partial convert everything? – Veger Jan 14 '10 at 18:11