My suggestion is to spend more time studying the word que and how it's used with prepositions and in relative clauses. It's beyond the scope of this article, but the more deeply you understand how other prepositions can hang on to que in terms like en que and al que, the easier you'll find it to parse sentences where por is attached to a que.