I have a legacy PHP application that uses the following URL structure
/product_info.php?products_id=YYY
where YYY is the product’s ID, Product model’s id field.
I am now rewriting this application using Rails and for SEO purposes, I have to keep this URL structure for now.
How should I route the above URL to my ProductsController’s show action? Should I use Apache’s mod_rewrite to rewrite it to /products/:id or can I do it with Rails’ router? I would love to be able to use path helpers so that product_path(@product) returns me /product_info.php?products_id=YYY (again for SEO)
This is actually more straightforward than I expected it to be.
First, set up your route:
Since Rails doesn’t normally serve PHP files, you’ll need to create a MIME type handler for
.php:Set up your
products#showaction to find the product based on the URL parameter. Since you aliased thephpMIME type totext/html, you don’t have to do anything special to render the “PHP” view:You’ll have to manually create the
product_pathhelper method, since you’re not using RESTful routes:Now just create your view: