Reroute old content (.html/.php etc.) to Ruby on Rails

前端 未结 3 1124
没有蜡笔的小新
没有蜡笔的小新 2021-01-14 04:31

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 ex

相关标签:
3条回答
  • 2021-01-14 05:11

    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

    0 讨论(0)
  • 2021-01-14 05:20

    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.

    0 讨论(0)
  • 2021-01-14 05:27

    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.

    0 讨论(0)
提交回复
热议问题