[1.7.x] Fixed #23583 -- More selectively ignored static/media roots

Fixed a regression introduced by 28efafa24c.
Thanks Michal Čihař for the report and initial patch, and
Collin Anderson and Tim Graham for the reviews.
Backport of 8b4cc9df9c from master.