Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save paulmwatson/c5c669f5478244ebadea12d3b0087103 to your computer and use it in GitHub Desktop.
Save paulmwatson/c5c669f5478244ebadea12d3b0087103 to your computer and use it in GitHub Desktop.
A Rails route.rb rule that will redirect any GET to another domain and maintain the path, query, and hash/fragment.
Rails.application.routes.draw do
get '(*path)', to: redirect{|params, request| "#{ENV['DOMAIN_TO_REDIRECT_TO']}#{request.url.gsub(request.scheme, '').gsub('://', '').gsub(request.host + '/', '')}"}
end
@paulmwatson
Copy link
Author

You can do this with .htaccess or nginx or Apache config but a special set of circumstances forced me to use a Rails app. Could possibly be a Ruby/Sinatra script too but again, special circumstances. Happy to hear improvements to the code itself.

@paulmwatson
Copy link
Author

A nice improvement would be maintaining the original sub-domain but that means you also need a wild-card SSL certificate. Heroku, which this route.rb is hosted on for my purposes, doesn't allow wild-card custom domains over SSL.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment