Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

construct url without subdomain in rails 3

I'm currently detecting whether a user has a certain subdomain and redirecting the user to the url without the subdomain as such:

subdom = request.subdomains.first
if subdom == "redirectme"
  redirect_to(request.protocol + request.domain + ":" + request.port.to_s)
end

As you can see, I'm reconstructing the domain manually to not include the subdomain to replace something like: http://redirectme.example.com:3000 to: http://example.com:3000

However, I have to keep in mind that sometimes there won't be a port (and so the colon after the domain is redundant) and I suspect there are other things I didn't account for. What's a better way of getting the full domain without its subdomain component?

Thanks!

like image 725
Yuval Karmi Avatar asked Nov 27 '22 14:11

Yuval Karmi


2 Answers

How about root_url( :subdomain => false )?

like image 78
Jasper Kennis Avatar answered Dec 05 '22 10:12

Jasper Kennis


Actually i don't know if there are better ways. As for the components, you pretty much got it: protocol, domain and port.

If you want the colon gone, you can do like this:

redirect_to(request.protocol + request.domain + (request.port.nil? ? '' : ":#{request.port}"))

Just a nil check that performs the magic!

like image 30
Lucas d. Prim Avatar answered Dec 05 '22 11:12

Lucas d. Prim