Codii
Codii

Reputation: 873

URL rewriting fails because of url encoding

I recently implemented versioning on my API project. Now URLs of my api will have to be prefixed by the sequence /v1/ (As I'm currently on v1)

I don't want to break old calls, so to ensure transparent compatibility, I want to rewrite the old urls.

Url like /events/9999/attendees should be redirected to /v1/events/9999/attendees

I set this up with my rails routes config file routes.rb with the following code :

match "*path", :to => redirect("/#{API_CONFIG['current_version']}/%{path}"),
:constraints => lambda { |request| true }

The issue I'm facing now is that the final rewritten route seems to be url-encoded ... (or something) Actually, that's the %{path} that seems to be broken. Then :

/events/9999/attendees is redirected to /v1/events%2F9999%2Fattendees

What am I doing wrong ?

Many thanks

Upvotes: 2

Views: 287

Answers (1)

Orlando
Orlando

Reputation: 9692

you can pass a block to redirect to specify the correct url

match "*path", :to => redirect {|params, request| "/v1/#{params[:path]}"}, :constraints => lambda { |request| true }

Upvotes: 2

Related Questions