If you ever have a need to programmatically write to the .htaccess file, WordPress has a built-in function insert_with_markers to do just that. Calling that function will give you the added benefit of adding “BEGIN” and “END” markers which makes it easier to read.
There’s also a sibling function, extract_from_markers, that can read from .htaccess, assuming it was written to using the first function.
I don’t have a specific need for this since I don’t usually host on Apache servers, but I see it come up from time to time, and the functions aren’t obviously named, so I thought I note it.