# How to Get the Current Absolute URL in Rails

*Published on September 07, 2022*

---


Every Rails controller includes two accessor methods pointing to the request and the response objects associated with the current HTTP request cycle. The&nbsp;`request`&nbsp;method returns an instance of the&nbsp;`ActionDispatch::Request`&nbsp;class, which wraps the HTTP request and provides a nice API to inspect the request.

**Use**&nbsp;**`original_url`**&nbsp;**method on the**&nbsp;**`request`**&nbsp;**object to get the current absolute URL.**

```ruby
# Returns the original request URL as a `String`

# get "/articles?page=2"

request.original_url # => "http://www.example.com/articles?page=2"
```

Here's the source code for this method.

```ruby
def original_url
  base_url + original_fullpath
end
```

The&nbsp;`base_url`&nbsp;returns the URL containing the protocol and the domain, and the&nbsp;`original_fullpath`&nbsp;returns a String with the requested path, including the query params. &nbsp;

If you don't care about the protocol and domain, use the&nbsp;`original_fullpath`&nbsp;or the&nbsp;`fullpath`&nbsp;method on the request. It returns the path including params.

```ruby
# get "/articles"
request.fullpath # => "/articles"

# get "/articles?page=2"
request.fullpath # => "/articles?page=2"
```

Here's some other information available on the request object.

<table><tbody><tr><th class="lexxy-content__table-cell--header" style="background-color: rgb(238, 238, 238);"><p><b><strong>Property of&nbsp;</strong></b><b><code><strong>request</strong></code></b></p></th><th class="lexxy-content__table-cell--header" style="background-color: rgb(238, 238, 238);"><p><b><strong>Purpose</strong></b></p></th></tr><tr><td style="background-color: transparent;"><p><code>host</code></p></td><td style="background-color: transparent;"><p>The hostname used for this request.</p></td></tr><tr><td style="background-color: transparent;"><p><code>domain(n=2)</code></p></td><td style="background-color: transparent;"><p>The hostname's first&nbsp;<code>n</code>&nbsp;segments, starting from the right (the TLD).</p></td></tr><tr><td style="background-color: transparent;"><p><code>format</code></p></td><td style="background-color: transparent;"><p>The content type requested by the client.</p></td></tr><tr><td style="background-color: transparent;"><p><code>method</code></p></td><td style="background-color: transparent;"><p>The HTTP method used for the request.</p></td></tr><tr><td style="background-color: transparent;"><p><code>get?</code>,&nbsp;<code>post?</code>,&nbsp;<code>patch?</code>,&nbsp;<code>put?</code>,&nbsp;<code>delete?</code>,&nbsp;<code>head?</code></p></td><td style="background-color: transparent;"><p>Returns true if the HTTP method is GET/POST/PATCH/PUT/DELETE/HEAD.</p></td></tr><tr><td style="background-color: transparent;"><p><code>headers</code></p></td><td style="background-color: transparent;"><p>Returns a hash containing the headers associated with the request.</p></td></tr><tr><td style="background-color: transparent;"><p><code>port</code></p></td><td style="background-color: transparent;"><p>The port number (integer) used for the request.</p></td></tr><tr><td style="background-color: transparent;"><p><code>protocol</code></p></td><td style="background-color: transparent;"><p>Returns a string containing the protocol used plus "://", for example "http://".</p></td></tr><tr><td style="background-color: transparent;"><p><code>query_string</code></p></td><td style="background-color: transparent;"><p>The query string part of the URL, i.e., everything after "?".</p></td></tr><tr><td style="background-color: transparent;"><p><code>remote_ip</code></p></td><td style="background-color: transparent;"><p>The IP address of the client.</p></td></tr><tr><td style="background-color: transparent;"><p><code>url</code></p></td><td style="background-color: transparent;"><p>The entire URL used for the request.</p></td></tr></tbody></table>

