What causes muscle spasms under right rib cage?

Diaphragm. It could be your diaphragm. Make sure you stretch your trunk muscles routinely, it could help.