Yes, there's a rule:
A relative pronoun is optional when the relative clause has a non-subject gap.
Every that-relative clause has a gap, a missing constituent that's linked to the modified noun phrase. The gap may be in the subject position:
I want a housei [that ___i is big and beautiful] (subject gap)
Note that the subject in the relative clause is missing, but it's anaphorically linked to the noun "house". When the gap is in the subject position, the relative pronoun is obligatory, at least in the standard English; hence, the ungrammatical sentence below:
*I want a house is big and beautiful
When the gap is not in the subject position (eg. the object position or the prepositional-complement position), the relative pronoun is optional:
I live in the housei [(that) my friend sold _____i to me] (object gap)
Since the missing constituent in the relative clause is the direct object of the verb "sold", the omission of the relative pronoun won't make the sentence ungrammatical.