Does бы have to follow immediately after если?
No, not at all.
Considering a phrase
Если бы ты тогда пошёл в театр...
б can be moved almost anywhere¹, except in the very beginning of the sentence and in-between a preposition and its noun. All the sentences below are grammatically acceptable:
Если б ты тогда пошёл в театр...
Если ты б тогда пошёл в театр...
Если ты тогда б пошёл в театр...
Если ты тогда пошёл бы в театр...
Если ты тогда пошёл в театр бы...
б at the very end makes it sound somewhat clumsy, like if you've decided what exactly you want to say at the very last moment.
Is there a difference or preference as to where to put
Since one can put an emphasis on different words in a sentence, stressing
ты to express that it should've been you, as opposed to someone else
тогда - then, as opposed to some other date or time
пошёл - given some context it could be walk, as opposed to drive, but in general it is went, as opposed to e.g. stayed home
в театр - to the theater, as opposed to somewhere else
бы next to (either before or after) the stressed word makes it sound more natural and in texts - when the choice of intonation is up to a reader - placing
бы in front of the stressed word helps writer convey the intended intonation.
Now if you wonder about the difference of
б - grammatically there is none. Commonly
бы would be used if the next word starts with
б; some people would use
б if the next word starts with a vowel and
бы otherwise, but those aren't rock-solid rules. The choice is mostly made based on the preference of a speaker or, say, the foot of a verse.
(¹) Upd: You can exploit the "free word order" even further and move
если around, making the phrasing more casual and somewhat archaic. Same rules as with
бы apply, but
если can appear in the beginning of the sentence.
Если can even switch positions with
бы, though if you put some distance between
если it will delay the point where the reader or listener will be able to distinguish a conditional phrase from a suggestion statement (one without
если), making it sound somewhat weird.