55

If I'm in a URL such as

http://domain.example/mysite/bla

How can I request just the URL with no paths? Such as

http://domain.example
Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
Martin
  • 11,216
  • 23
  • 83
  • 140

7 Answers7

119

You can use this

<%= request.protocol + request.host_with_port %>
#=> https://domain.example:3000
<%= request.protocol + request.host %>
#=> https://domain.example

Starting from Rails 3.2 you can also use

<%= request.base_url %>
#=> https://domain.example:3000
Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
fl00r
  • 82,987
  • 33
  • 217
  • 237
5

request.host should do the trick, or:

request.port.blank? ? request.host : "#{request.host}: #{request.port}"

if you need to include the port too.

Craig Ringer
  • 307,061
  • 76
  • 688
  • 778
Pravin
  • 6,592
  • 4
  • 42
  • 51
4

Try this

<%=request.scheme + '://' + request.host_with_port%>

If you want to see all available methods on request object then

<%=request.methods.sort%>
Ashish
  • 5,723
  • 2
  • 24
  • 25
2

For protocol, domain and port

<%= "#{request.protocol + request.host}:#{request.port.to_s}" %>
Kulbir Saini
  • 3,926
  • 1
  • 26
  • 34
1

Here's how you can get your current domain name.

Domain name

request.base_url 
# prod: https://example.com
# dev: https://localhost:8000

Getting full path

request.original_url
# prod: https://example.com/path/
# dev: https://localhost:8000/path/
0

If your port could be anything other than 80 it should be included.

"#{request.protocol}#{request.host_with_port}"
Rohanthewiz
  • 947
  • 9
  • 9
0

I think this can be useful too, if you're not in a controller.

URI.join(url_for(only_path: false), '/').to_s 
kangkyu
  • 5,252
  • 3
  • 34
  • 36