OpenIG – Apache rewrite 302

This topic has 2 replies, 2 voices, and was last updated 4 years, 10 months ago by jochenr.

  • Author
    Posts
  • #17957
     jochenr
    Participant

    Hi,

    I’m looking into an Apache look-a-like rewrite on OpenIG using the StaticResponseHandler.
    For basic redirects and rewrites this is working fine, but I need something more specific, so that all path and query attributes are passed to the Location where it need to goto.

    So on Apache it should be something like this:

    Rule FROM: https://www.example.com/support/en/manuals [/*] [%params%]
    Rule TO: https://emanuals.example.com/support/en/manuals [/*] [%params%]

    On IG level I’ve configured the following:

    {
    	"name": "10-support-rewrite",
    	"handler": {
    		"type": "DispatchHandler",
    		"config": {
    			"bindings": [{
    					"condition": "${matches(request.uri.path, '^/support/en/manual.*')}",
    					"handler": {
    						"type": "StaticResponseHandler",
    						"config": {
    							"status": 302,
    							"reason": "Found",
    							"headers": {
    								"Location": ["https://emanuals.example.com${urlEncodeQueryParameterNameOrValue(request.uri.path)}?${urlEncodeQueryParameterNameOrValue(request.uri.query)}"]
    							},
    							"entity": "Redirecting..."
    						}
    					}
    				},
    				{
    					"handler": {
    						"type": "Chain",
    						"config": {
    							"filters": [{
    								"type": "HeaderFilter",
    								"config": {
    									"messageType": "REQUEST",
    									"remove": ["host", "x-forward-host"],
    									"add": {
    										"host": ["www.example.com"]
    									}
    								}
    							}],
    							"handler": "ClientHandler"
    						}
    					}
    				}
    			]
    		}
    	},
    	"baseURI": "http://internal.example.com:8080",
    	"condition": "${matches(request.uri.path, '^/support.*')}"
    }

    As you can see I’m using ${urlEncodeQueryParameterNameOrValue(request.uri.path)}?${urlEncodeQueryParameterNameOrValue(request.uri.query)}

    But when only a path is used, it’s appended with a ? in the end.
    Is there a way that path and query or both are passed through on a specific way ?

    Thanks in advance.

    #19677
     chris-fry
    Participant

    I might be missing something, but if you just want a rewrite (without path transformation), this should do it:

    {
      "baseURI": "http://emanuals.example.com",
      "condition": "${request.uri.host eq 'www.example.com'}",
      "handler": "ClientHandler"
    }

    If you want a redirect (302), this is probably more what you want. It redirects requests to http://www.example.com/{x} to emanuals.example.com/{x}:

    {
      "handler": {
        "type": "StaticResponseHandler",
        "config": {
          "status": 302,
          "reason": "Found",
          "headers": {
            "Location": [
              "http://emanuals.example.com${request.uri.path}"
            ]
          }
        }
      },
      "condition": "${request.uri.host eq 'www.example.com'}"
    }

    – Chris

    #19731
     jochenr
    Participant

    Hi,

    Thanks for the update but it’s not what I was looking for. The solution you gave is working fine and without any issues, but when using this, the query behind the path is been dropped when redirecting.

    Example:

    Request without query: https://www.example.com/manuals/test -> 302: https://manuals.example.com/manuals/test
     Request with query: https://www.example.com/manuals/test?test=true -> 302 https://manuals.example.com/manuals/test
    

    That’s why I added request.uri.path?request.uri.query so when a query param is given this is also been fetched, but when no query exists the ? stays there.

    Request without query: https://www.example.com/manuals/test -> 302: https://manuals.example.com/manuals/test?
     Request with query: https://www.example.com/manuals/test?test=true -> 302 https://manuals.example.com/manuals/test?test=true
    

    For the moment the I rewrote some things in a groovy script to accomplish this.

    – Jochen

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.

©2022 ForgeRock - we provide an identity and access platform to secure every online relationship for the enterprise market, educational sector and even entire countries. Click to view our privacy policy and terms of use.

Log in with your credentials

Forgot your details?