I've always felt too limited by standard harmony. That's why I've been listening to a lot of jazz and fusion lately. I feel too restricted if I'm going to write a song in just one tonality, and sometimes it sounds a bit dull to me.

I know about non-diatonic chords and such (not much though), but what I mean is a different tonality over each chord. In a song I've been working on lately, the tonality changes with each chord, and the chords play for several bars each (4 bars in most parts). I have a funky blues pentatonic intro to the song, and then right after two chords play for 4 bars each, A7 and Gm7. Over A7 I used the pentatonic of A Mixolydian, and over the Gm7 I used G Dorian, and the whole thing repeats once more. The thing is that it didn't sound "wrong" at all. Actually to me it sounded way more interesting than the one tonality stuff I've always written. Then after this part I play an A Phrygian dominant riff for a while, since I wanted a Middle Eastern feel to it. The transition sounded smooth to me since it's the same root chord. And after that I'm thinking to repeat the previous part, then right after a different Phrygian dominant melody (that I still haven't written).

As for the solo, this is the chord progression I've come up with just yesterday: A7, Dm7, A7, E7, C#7, E7 and then as an outro to the solo an A Phrygian dominant section for 8 bars. In the first section of the solo, over the A7 I used A minor pentatonic to give it a bluesy sound. Over the Dm7 chord I played D minor pentatonic then D natural minor. The change of tonality sounded much more interesting than sticking to one tonality, and gave the solo some soul or something. As for the rest of the solo I still need to come up with some ideas.

My question is: do I really have to stick to harmony and to just one tonality for riffs/melodies and follow the rules strictly? I feel this is very limiting. As you can see especially with the solo I wasn't following a certain diatonic chord progression, but the chords just sounded good this way and allowed me to be more free with my solo and riffs. Is what I've written considered "wrong?" Also if someone has a theoretical explanation to what I've done it would be more than appreciated.

You can play music for yourself, you can play music for other musicians, or you can play music for the general public. If you are good enough, you will get the audience you asked for.

All three audiences want a combination of being able to predict what is going to happen next and be surprised by what happens next.

It's like playing with a cat using a toy on the end of the string.

If the cat catches the string too often it gets bored and quits playing. If you never let the cat catch the toy, it gets bored and quits playing.

If the audience can predict what's coming next too often, it gets bored and quits listening. If the audience cannot or can seldom predict what's coming next, it gets bored and quits listening.

Now there are degrees of predictability and surprise for different listening groups. That's where your judgement comes in. The Jazz listener has better prediction skills and is harder to surprise than the EDM, Blues or Country audience. So what works for one, will not necessarily work for the other.

Listen and analyze to what your targeted audience listens to, and that will help a lot. Of course, if you are just writing songs for yourself, you are the audience.

Music is free expression and you can pretty much do what you want. But I think the more you go outside the box, the more critics you will have. When I listen to someone like Allan Holdsworth I'm often cocking my head to the side wondering how the notes align to the chord progressions. Standard progressions make more sense to me. They just feel more alive in the song. But that doesn't mean that people who stray away from this norm are wrong. It's just not my particular flavor. But at the same time, when I'm playing and writing I like to try to stray away from the norm sometimes, too, so long as it's pleasing to my ear. And ultimately, I think that is what it boils down to: Do you like what you're doing? If so, do it. Free expression, have fun, and try not to worry about what others think.

In art, the more abstract an artist gets, the smaller his/her audience gets. Not a bad thing as long as your audience is enough for you.

Some people handle dissonances with such skill that many people don't even think of them as dissonances. In my mind right now I have Dave Brubeck, Thelonius Monk, Dmitri Shostakovitch and Serge Prokofiev.

Yet I was at a concert by a world famous symphony orchestra and heard people complaining about a Shostakovitch symphony. It was too much for their understanding of music.

While scales, key centers, modes, and all the other aspects of music need to be understood, the music should rule - not the analytical brain. To put this mode or change to this key or whatever because your brain thinks it might be a good idea, but when you try it, if it doesn't sound musical, discard it. Another idea might be better.

In a song I've been working on lately, the tonality changes with each chord, and the chords play for several bars each (4 bars in most parts).

Sounds like a description of vintage modal jazz to me.

I have a funky blues pentatonic intro to the song, and then right after two chords play for 4 bars each, A7 and Gm7. Over A7 I used the pentatonic of A Mixolydian, and over the Gm7 I used G Dorian, and the whole thing repeats once more. The thing is that it didn't sound "wrong" at all.

"If it sounds right, it is right." - Music Rule #1. (There are no other music rules .)

In fact, you're not too far removed from traditional tonality here. A7 and Gm7 both belong in key of D minor. (You're subverting that a little with A mixolydian, although I'm not sure what the "pentatonic of A mixolydian" is...)

Actually to me it sounded way more interesting than the one tonality stuff I've always written. Then after this part I play an A Phrygian dominant riff for a while,

Back in the D minor ballpark. That's the D harmonic minor scale, of course. One note different from G dorian (= D natural minor).

As for the solo, this is the chord progression I've come up with just yesterday: A7, Dm7, A7, E7, C#7, E7 and then as an outro to the solo an A Phrygian dominant section for 8 bars. In the first section of the solo, over the A7 I used A minor pentatonic to give it a bluesy sound. Over the Dm7 chord I played D minor pentatonic then D natural minor. The change of tonality

What change? A minor pent and D minor pent are both subsets of D natural minor (of which G dorian is a mode of course).

As for the rest of the solo I still need to come up with some ideas.

Right! The E7 and C#7 are where it gets more interesting. Much less tonal.
The E7 could be explained functionally as V of A7 - a secondary dominant if the key is D minor, but the primary dominant if you're viewing A as keynote (which seems reasonable).
The C#7 is the odd one out, although there is still a traditional term for it (relative to key of A) if you want one: "chromatic mediant".

As it is, the whole thing could be seen as an interesting example of mode mixture (modal interchange) with all the chords coming from various A-root scales.
A7 = A mixolydian (or phrygian dominant)
Gm7 = A phrygian
Dm7 = A aeolian or phrygian
E7 = A major, A harmonic minor, A melodic minor, or A harmonic major
C#7 = A ionian augmented (mode of F# harmonic minor), or A harmonic major (if you read the E# as F).

My question is: do I really have to stick to harmony and to just one tonality for riffs/melodies and follow the rules strictly?

Music theory is not rules you have to stick to. It's just descriptions of common practices.
And - as you see above - it offers various descriptions of everything you're doing (so far), so you're not "breaking any rules" here. Some of the practices you're following are not that common (the C#7 at least), but they are all in the theory books. (Although maybe not all in the same theory books... )

I feel this is very limiting.

You're limiting yourself. Don't blame theory! Your deviations from tonality are very tentative, as if you're just dipping your toe in a strange pool...
(Limitation is good anyway. There is no art without limitation.)

Your ear is the final judge, always. Remember rule #1. Dive into that pool!
If you're not familiar with modal jazz (which could be your inspiration here) check out Miles's Kind of Blue (especially Flamenco Sketches), Herbie Hancock's Maiden Voyage, or various Wayne Shorter tunes (Ana Maria is a good one).

Seems to me like you are describing modal playing. Suprisingly, there are alot of advanced and interesting modes out there that are widely popular in certain genre within commercial music. Phrygian dominant is very popular in metal, flamenco. Sometimes, I hear mixolydian b6 being used to substitute over the secondary dominant that resolves to a minor chord in southern rock.

Someone said and I agree strongly, ear is final judge. (many good, pro guitarists, say it, if it sounds good it is good)

Yes, the tonality changes with each chord, that's why they are there. Unless, a vocal lead line is one note, over and over, finished by two or three different notes at the end, tonality and scale will change. That's how you start picking out melody lines on the fly, finding the tonality.

Now that I've been playing 8 years very seriously, modes, patterns, and visualising the chord of the moment, take a back seat. I have learned them thoroughly, but I play by ear now and only use the scales to take the pressure off with knowing where I'm at. So on the occasion, where I lose it and get lost in my thoughts, I use them for a home base.

As a pretty good guitar teacher said to me, take them anywhere you want, just bring them home. So to a 1, 3, 5 of the chord. That's limiting in its self, but you get more creative than that.

Mine is not a very theory answer, more of a practical playing one. To be honest you have to be able to know what the note sounds like before you play it, and once you get there you are not seeing shapes or modes, you are just seeing 4 frets with notes, all of which you know the sound of.