Is it necessary to check for `content-type` in headers?

In the context of that question, yes it is. Without inspecting the content type header you don’t know if what is being served is an html page or an image, and while it is unlikely that the regex will match anything in an image, the risk is there.

a different way to write that code is to hook it on template_redirect or even get_header to be as close to 100% sure you are dealing with html pages and not RSS, json, xml etc, for which that URL transformation might not be required or even welcome.

looking again at the original question, obviously you will want it in the admin pages as well… so maybe your original solution is the most comprehensive that can actually be.

OTOH, and not very related to this question, you might end with replacing too much, like the canonical url which might create duplicate content problem.

tech