Rails routes: redirect a wild card route - ruby-on-rails

I have a website built in Ruby on Rails and a blog built in WordPress. The url for the main site will be example.com and the url for the blog is blog.example.com. Because of the way search engines index sites and the preference for a directory blog as opposed to a subdomain blog, I want to implement a permanent redirect so that example.com/blog/anything will redirect to blog.example.com/anything regardless of how many slashes or parameters the url contains. So even example.com/blog/a/b/c/d/e?google=true should redirect to blog.example.com/a/b/c/d/e?google=true
So far the following works if there is only one directory after blog:
get '/blog/:what', to: redirect('http://blog.codeundercover.com/%{what}')
I, however, need this to work regardless of what text comes after /blog. How do I do this?

Your route is not using a wildcard route. Instead, it's what the Rails Routing Guide refers to as a Static Segment
You'll want to use Wildcard Globbing instead:
get '/blog/*what', to: redirect('http://blog.codeundercover.com/%{what}'), constraints: { what: /.*/ }

get "/blog(/*what)", to: redirect("http://blog.codeundercover.com/%{what}")

Related

Rails 4 Routing within Namespace

I am trying to set the base address www.myplace.com/admin to be directed to www.myplace.com/admin/adminhub within the namespace coding below. I have tried every combination I can think of, but to no avail. I was trying to follow the same code used for the / of the app.
namespace :admin do
...
get "adminhub"
get 'admin', to: 'adminhub'
end
Sounds like you want to redirect the browser to the new URL. You can do this with the redirect helper in the routes.
get '/admin', to: redirect('/admin/adminhub')
This allows you to redirect from one path to another. See the Redirection section in the Rails Routing Guide for more details.

*_url helpers don't work with varying subdomain levels

I've got a site with an api subdomain. On the same domain where the site is located, I've also got a dev subdomain with, you guessed it, a development version of the site.
My issue is that this dev version of the site also has an api subdomain (ie: api.dev.example.com. The routes work without issue using a regex to match the last segment of the subdomain against api (/api(?:\..*)?/), but the *_url helpers do not generate what I would like. Instead of spitting out http://api.dev.example.com/somepath, they spit out http://dev.example.com/somepath.
I'm looking for a solution that doesn't require special handling of the dev site vs the prod site. Subdomain sniffing based on requests so some such nonsense does not appeal to me.
Relevant routes.rb content:
constraints subdomain: /api(?:\..*)?/, format: 'json', protocol: 'http://' do
get '/' => 'example#index'
post '/create', to: 'example#create', as: :api_create_example
end
You can pass the subdomain to the url helper:
fear_and_loathing_in_las_vegas_url(subdomain: 'api.dev')
This will generate http://api.dev.example.com/fear_and_loathing_in_las_vegas

How to handle routing error in Rails 4 with alchemy cms gem?

So in our Rails 4.2 application, there is the alchemy_cms gem which requires its routes to be mounted last in config/routes.rb.
SampleApp::Application.routes.draw do
#other routes for the rails app here
# :
# :
mount Alchemy::Engine => '/'
end
We get routes like "/somehacker/routingurl" which then falls out to the Alchemy::Engine to handle, resulting in a default 500 error. If I wanted to do a custom 404 error, then the proper way to do it is to build a custom 404 page and have Alchemy handle the redirect? Otherwise since the Alchemy docs specify that it has to be the last route in config/routes.rb, there won't be a way to add a catchall route to redirect to some kind of error page I assume.
EDIT:
One of the problems is that there are some routes that are like the invalid "somehacker" route above that do need to be parsed by the Alchemy routing engine, such as "/en/us" where "en" is a valid locale. This is why I initially thought to put the route handling in the Alchemy engine's routes file.
If it is difficult for you to configure and use the Alchemy cms gem to redirect unknown routes into a custom defined page, you can use the bellow method to implement the same with a small coding tweak given bellow:
Rails 4.XXX
1. First Method.
(routes.rb)
You can still use a simple get to redirect all unknown routes.
get '*path', to: 'home#index'
If you wish to provide routing to both POST and GET requests you can still use match, but Rails wants you to specify the request method via via.
match "*path" => "home#index", via: [:get, :post]
Remember that routes.rb is executed sequentially (matching the first route that fits the supplied path structure), so put wildcard catching at the bottom of your matchings.
Here you can replace the home#index with any custom path that you defined in you application, also note that it is important to keep this redirection code only at the bottom of routes.rb.
2. You can follow the bellow tutorial on the same problem in a different perspective to solve can be found.
Custom 404 error page with Rails 4

Routing a folder to another URL

I Recently migrated a website from PHP to rails. NEwt to the website they was a wordpress that is staying. the urls were as follow :
www.thedomain.com / #former PHP now rails
www.thedomain.com/blog/ #the wordpress.
now that the website is in Rails, i cannot just put a php wordpress in the blog folder.
I tried to go to a blog.thedomain.com solution using thoses route (actual domain names i use in dev as i am trying to make it work) :
namespace :blog do
match'', to: redirect {|params,request| "http://blog.fakefake1111/#{params[:path]}#{request.path}"}, via: :all
match '*path', to: redirect {|params,request| "http://blog.fakefake2222/#{params[:path]}"}, via: :all
end
It doesnt quite work, as the params are ignored and the first one add /blog/ at the end of the URL.
How can i make it work?
I will use the alias capability of apache to do it. I do not really need to use rails for that.

Rails redirect 301

I have some touble with redirect 301 in my new app. I have to redirect some old urls into the new one.
I entred in my routes file this
match "/traslochi_puglia/index.htm", :to => redirect("/preventivo/90-traslochi-in-puglia")
and it works fine, but I can't understand why this
match "/trasloco_casa_abitazione.htm", :to => redirect("/3-trasloco-casa")
does not work. All the old urls with this pattern "/some_path/page.htm" works fine but not "page.htm". Any hint?
Thanks
If you want us to troubleshoot the specific issue you've outlined in your question, we need to see your entire routes.rb file. Without this information, my first guess is this:
The typical route pattern is /controller/action or /controller/:id/action or some combination thereof. With the pattern you've shown above, and assuming you have no named routes in your routes.rb file, then the route you've provided would point to a controller, but not an action. Therefore your app wouldn't know what action to execute, unless you've specifically created a route called /3-trasloco-casa which looks to me more like a URL to a specific resource, than an action on a controller.
Getting to the source of routing issues can most easily be done with a combination of running rake routes at the command line (which shows you the list of route patterns your app will recognize), and then going further by troubleshooting with route recognition, as outlined in this answer to this question:
Recognize routes in rails console Session

Resources