How did loophole become associated with law
...and stick almost exclusively to it?
According to Collins Dictionary:
A loophole in the law is a small mistake which allows people to do something that would otherwise be illegal.
The Grammarist notes that:
Interestingly, the word loophole goes back to the sixteenth century and refers to an architectural feature. In castles of the time, narrow slits were built into the walls where archers could shoot at attackers. These narrow slits were known as loopholes, most probably derived from the Dutch word lûpen meaning to watch.
The term loophole came into use in the seventeenth century in a figurative sense to mean a small opening or a outlet of escape.
How and when did loophole become associated with law?
How old is the expression Every law has its loophole?
From WorldWideWords
Q From Will Thomas: Where do we get loophole from?
Around the middle of the following century loophole began to be used figuratively for a means of escape and by 1700 could have our modern sense of an ambiguity or inadequacy in rules or laws that allows somebody to evade their provisions.
A loophole is an accidental technicality or unclear section of a written legal document that allows someone to avoid following a rule or fulfilling an contractural obligation. If you've discovered a way to get out of paying taxes on money you made last year, you've found a loophole. My sense is the legal sense occurred on or about the time referenced in the World Wide Words citation.