# How to Redirect Incoming HTTP Requests Using Rails Router

*Published on July 02, 2024*

Tags: Router

---


> This post is part of my&nbsp;[handbook on the Rails Router](/tag/router).

The&nbsp;[`redirect`](https://api.rubyonrails.org/v7.1.3.2/classes/ActionDispatch/Routing/Redirection.html#method-i-redirect)&nbsp;helper method lets you redirect any path to another path or URL. For example, the following route redirects all the incoming requests on&nbsp;`/images`&nbsp;to&nbsp;`/photos`.

```ruby
get "/images", to: redirect("/photos")

# OR

get "/images" => redirect("/photos")
```

If the older route contained&nbsp;[dynamic segments](/rails-routing-dynamic-segment-query-string), you can reuse them with interpolation, as follows:

```ruby
get "/images/:tag", to: redirect("/photos/%{tag}")
```

By default, the Rails router uses HTTP status&nbsp;[301 Moved Permanently](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/301)&nbsp;for redirecting.

💡 The HyperText Transfer Protocol (HTTP)&nbsp;**`301 Moved Permanently`**&nbsp;redirect status response code indicates that the requested resource has been definitively moved to the URL given by the&nbsp;[`Location`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Location)&nbsp;headers. A browser redirects to the new URL and search engines update their links to the resource. -&nbsp;[MDN](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/301)

To change the response status code, pass the&nbsp;`:status`&nbsp;option.

```ruby
get "/images/:tag", to: redirect("/photos/%{tag}"), status: 302
```

**Note:**&nbsp;Rails will use the default host if you don't provide one. To redirect to an external URL, provide the complete URL, including the host name.

```ruby
get "/blog", to: redirect("https://www.writesoftwarewell.com/")
```

