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!
How about root_url( :subdomain => false )
?
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With