Rewrite rules – Common Problems

23 Jun 2021, updated: 29 Dec 2021

Rewrite rules are an awesome tool but there are a few gotcha’s you should be aware of when using them. This post might save your future self hours of futile debugging!


# Using multiple Rewrite rules

Rules are independent

  • Unfortunately Rewrite Rules are totally independent of each other
  • For example, assume you are trying to generate a 404 error response (status code and body together). See screenshot
  • If, for example, the status code actually comes back as 403, the Body value will still be changed
  • This problem is common when working with the OPTIONS HTTP method

# Unescaped characters

“$” symbol in Response body

  • If you have an unescaped “$” in your Response body value, the response can sometimes fail to complete
  • See full detail of the problem this Stack Overflow post

Escaping a character

  • Simply put a “\” character in front of the “$” symbol

# Rewrite Rules and the OPTIONS method

Dealing with OPTIONS

  • Using Rewrite rules on endpoints with an OPTIONS HTTP method can be especially tricky
  • As mentioned previously, Rewrite rules are independent of each other
  • See more detail the problem on Stack Overflow post

The workaround for GET and POST

  • If you need to modify the Response body of the related POST or GET request you can add a rule to only match on response with bodies i.e. a OPTIONS response doesn’t contain a body
  • Add either \{[\S\s]*\} or \[[\S\s]*\] into the “Value” textfield of the “Match” section

# Debug with “Error Log”

Enable the “Error Log” feature

  • You can view additional logging details if you enable “Debug in Error Log” on the Rewrite screen

Open Error Log screen

  • To view the error logs go to Window -> Error Log

Simple example

  • In the following example, I am using a Rewrite to redirect the NY Times fav icon url to now fetch the Qantas fav icon instead
  • You can see there are 2 records in the Error Log screen for each Rewrite rule

Leave a comment