shadow

How to escape curly braces {} for rewrite in web.config

One must be careful here, because there is quotes escaping within the string, there is regex escaping, and there is string formatting in action node, which all together can tet bit confusing.

When matching, curly braces are excaped like this \{ and \}
When rewriting, curly braces are outputted like this {UrlDecode:%7B} and {UrlDecode:%7D}

Example

If your url to image is not in plain src attribute on image tag, it can nowadays look something like this:

Matching pattern needs to be somewhat more complex:

Pattern above will pick up everything before data-image attribute, match only “/images/nice-photo.jpg”, and then pick up everything after it. Now you are ready to rewrite image url to https://myimagesserver.com/images/nice-photo.jpg.

 

All together

Considering you are ASP.NET developer, you probably worked by now with rewriting rules in web.config. Just to make it easier for beginners, I will finish with couple of basic information:

This is the structure you need in your web.config in order to have rewriting in place

 

Finished rule should look like this

For the end, couple of advises:

  • when possible always rewrite before cache
  • too complex and too many outbound rules can impact performances when there is lot of html to process
  • least wildcard matching possible, least complex regex possible

Happy rewriting!