spring-cloud-gateway icon indicating copy to clipboard operation
spring-cloud-gateway copied to clipboard

Support to redirect with initial request path to the redirected request in RedirectTo filter.

Open KartikPandit opened this issue 2 years ago • 2 comments

Problem: The current RedirectTo filter, takes in the following config:

spring:
  cloud:
    gateway:
      routes:
      - id: prefixpath_route
        uri: https://example.org
        filters:
         - RedirectTo=302, https://acme.org, true

With current approach when redirection takes place it sets the response status to '302' and sets the 'Location' header to the provided URI, and also provides an option to include query param from the initial request. But this implementation drops the request path of the original request. Ex: Initial Request: GET http://localhost:8080/target/get?query1=value1 Redirected Request: GET https://acme.org?query1=value1

With the current implementation the initial request path is /target/get is dropped.

Proposed Solution: Provide an option similar to includeRequestParams to indicate whether the initial request path should be included on the url. The parameter is treated as false when not set to preserve backwards compatibility.

KartikPandit avatar Aug 12 '24 06:08 KartikPandit

This is similar to what Apache mod_alias Redirect directive does, so I am thinking of similar solution, which supports adding request path to the redirected request. https://httpd.apache.org/docs/current/mod/mod_alias.html#redirect

KartikPandit avatar Aug 12 '24 13:08 KartikPandit

Hi @spencergibb, @ryanjbaxter

I've submitted a PR (#4372) addressing this enhancement.

The implementation introduces an optional includeRequestPath parameter to the RedirectTo filter, allowing the original request path to be appended to the destination URL. It also supports forwarding query parameters alongside the request path while preserving the existing behavior by default.

I've added tests covering the default behavior, path inclusion, query parameter forwarding, and trailing slash handling.

Feedback and suggestions are welcome. Thank you!

yashdotdev13 avatar Nov 13 '26 08:11 yashdotdev13