# Rewriting multiple values of HTTP Location header

**URL:** <https://community.f5.com/t/rewriting-multiple-values-of-http-location-header/69150>\
**Category:** Technical Forums\
**Tags:** application-delivery\
**Created:** [January 31, 2022, 1:13pm UTC](https://community.f5.com/t/rewriting-multiple-values-of-http-location-header/69150 "2022-01-31T13:13:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lemaire\_Frédéri](https://avatars.discourse-cdn.com/v4/letter/l/f9ae1b/32.png) [@Lemaire\_Frédéri](https://community.f5.com/u/Lemaire_Fr%C3%A9d%C3%A9ri)\
**Post date:** [January 31, 2022, 1:13pm UTC](https://community.f5.com/t/rewriting-multiple-values-of-http-location-header/69150/1 "2022-01-31T13:13:06Z")

</div>

Hello

I’m trying to rewrite a HTTP location header in a response&nbsp; using a list of possible value to be rewrited

The response to be rewrited :

http://[myorighost:81](http://myhost1:81/test)/SASPortal/jsp/html/portal/portal.js

[http://myrewritedhost/analytics/Portal/jsp/](http://myhost1:81/test)html/portal/portal.js

So, in the header location, both the host and the path /analytics/portal should be rewrited.

How can I test multiple value to be changed?

I tried this without success:

HTTP::header replace Location [string map -nocase [list “[myorighost:81](http://myhost1:81/test)” “myrewirtedhost” “/analytics/Portal” “/SASPortal/”&nbsp; “/valuex/” “/valuexrewrited”] [HTTP::header value Location]]

Thanks in advance.

Regards

Frédéric

---

<div class="post-metadata">

**Author:** ![CA\_Valli](https://avatars.discourse-cdn.com/v4/letter/c/9dc877/32.png) [@CA\_Valli](https://community.f5.com/u/CA_Valli)\
**Post date:** [January 31, 2022, 5:39pm UTC](https://community.f5.com/t/rewriting-multiple-values-of-http-location-header/69150/2 "2022-01-31T17:39:17Z")

</div>

Hello Frédéric, do you need a redirect from first URL to second URL?

Will the two URLs always be the same or is there a whole subfolder content to be redirected?

In first case, in my opinion a LTM policy configured for static redirect will just work fine.

In second case, there’s a few ways to do it depending on the requirements.

If there’s a big list of URLs to be handled, you might want to work with Data Groups to keep it simple. Configure a string-type DG like this:

```html
ltm data-group internal /Common/datagroup_path_rewrite {
 records {
 /oldsubfolder1/ { }
 /oldsubfolder2/ { }
 /oldsubfolder3/ { }
 /oldsubfolder4/ { }
 /oldsubfolder5/ { }
 }
 type string
}

```

Then use it in an iRule for path replacement

```html
when HTTP_REQUEST {

  if {[string tolower [HTTP::host]] eq "myorighost:81" } {
    HTTP::header replace Host "myrewritedhost"
    # this is not a redirect
  }

  # read as: does the uri contain an element of datagroup_path_rewrite (case sensitive)
  if {[class match [HTTP::uri] contains datagroup_path_rewrite]} {

    HTTP::path /analytics/Portal/jsp/html/portal/portal.js
    # this rewrites the URI path before senting traffic to BE server
  }
}

```

Also, I’ve recently answered a similar question in [this thread](https://community.f5.com/post/226134#M209828), see if this helps as well.

---

<div class="post-metadata">

**Author:** ![Lemaire\_Frédéri](https://avatars.discourse-cdn.com/v4/letter/l/f9ae1b/32.png) [@Lemaire\_Frédéri](https://community.f5.com/u/Lemaire_Fr%C3%A9d%C3%A9ri)\
**Post date:** [January 31, 2022, 8:51pm UTC](https://community.f5.com/t/rewriting-multiple-values-of-http-location-header/69150/3 "2022-01-31T20:51:06Z")

</div>

Hello,

Thanks for your answer.

In fact, I need to replace a reverse proxy with the F5, currently with LTM.

The problem I’m having is the rewriting of the RESPONSE and specifically the Location Header.  
For the HTTP request rewriting&nbsp; I was able to rewrite it with “internalhost” and internal uri/path to the back-end webserver.

The problem I’m having in the Response redirects coming from the back-end that I need to rewrite with external host + adapt some path and query params ( within the same redirect location )

How can I do that ?

for exemple :

Http location received from the back-end that need to be rewrited to

[http://internalhost/SASPortal/public http://ExternalHost/analytics/Portal/public](http://ExternalHost/analytics/Portal/public)

[http://internalhost/SASPortal/test?myparam=http%3ASASPortal…](http://ExternalHost/analytics/Portal/public)&nbsp; [http://internalhost/SASPortal/test?myparam=http%3%2F%2Fanalytics%2FPortal…](http://ExternalHost/analytics/Portal/public)

[http://internalhost/SASTheme/mytheme http://ExternalHost/](http://ExternalHost/analytics/Portal/public)[Theme\_extranal/mytheme](http://ExternalHost/analytics/Portal/public)

I also need to rewrite the referer with the external uri and some relative path in the location

For the Content rewriting of the response, I 'm using a stream profile and this is ok . But I’m having a lot of problems with the rewriting of the Location header.

many thanks in advance for your future answers.

Regards

Frédéric

---

<div class="post-metadata">

**Author:** ![Enes\_Afsin\_Al](https://d1p9zq3aats0t8.cloudfront.net/user_avatar/community.f5.com/enes_afsin_al/32/15359_2.png) [@Enes\_Afsin\_Al](https://community.f5.com/u/Enes_Afsin_Al)\
**Post date:** [January 31, 2022, 11:15pm UTC](https://community.f5.com/t/rewriting-multiple-values-of-http-location-header/69150/4 "2022-01-31T23:15:04Z")

</div>

Hi Frédéric,

Can you add default stream profile to vs and try this iRule?

```tcl
when HTTP_REQUEST {
	STREAM::disable
	if { [HTTP::host] eq "externalhost" } {
		HTTP::host "internalhost"
		switch -glob [HTTP::uri] {
			"/analytics/Portal*" { HTTP::uri [string map {"/analytics/Portal" "/SASPortal"} [HTTP::uri]] }
			"/Theme_extranal*" { HTTP::uri [string map {"/Theme_extranal" "/SASTheme"} [HTTP::uri]] }
		}
	}
}

when HTTP_RESPONSE {
	if { [HTTP::header exists "Location"] } {
		HTTP::header replace Location [string map [list "internalhost" "externalhost" "/SASPortal" "/analytics/Portal" "/SASTheme" "/Theme_extranal" "/valuefrominternal/" "/valuetoexternal/"] [URI::decode [HTTP::header Location]]]
	}
	STREAM::expression {@internalhost@externalhost@}
	STREAM::enable
}

```
