Is it permissible to rebel against the ruler?

There are people who think that because some of the rulers commit acts of kufr and sin, we are obliged to rebel against them and attempt to change things even if that results in harming the Muslims in that country, at a time when there are many problems in the Muslim world. What is your opinion?

Praise be to Allaah. 
The basic comprehensive principle of sharee’ah is that it is not permitted to remove an evil by means of a greater evil; evil must be warded off by that which will remove it or reduce it. Warding off evil by means of a greater evil is not permitted according to the scholarly consensus (ijmaa’) of the Muslims. If this group which wants to get rid of this ruler who is openly committing kufr is able to do so, and can bring in a good and righteous leader without that leading to greater trouble for the Muslims or a greater evil than the evil of this ruler, then that is OK. But if rebellion would result in greater trouble and lead to chaos, oppression and the assassination of people who do not deserve to be assassinated, and other forms of major evil, then that is not permitted. Rather it is essential to be patient and to hear and obey in matters of good, and to offer sincere advice to the authorities, and to pray that they may be guided to good, and to strive to reduce evil and increase good. This is the correct way which should be followed, because that is in the general interests of the Muslims, and because it will reduce evil and increase good, and because this will keep the peace and protect the Muslims from a greater evil.