ISTANBUL—President Abdullah Gul late Tuesday signed into law a bill giving the government sweeping powers to block websites and monitor user activity, a move critics assailed as further endangering Turkey's democracy ahead of elections that could reshape the political landscape.

Mr. Gul announced the move on his Twitter account, stressing he had agreed to sign only after government officials pledged to amend two disputed articles of the legislation.

The law, which Parliament approved this month, will give Turkey's communication ministry and the directorate monitoring telecommunication broad supervisory powers over Turkish Internet service providers, allowing the government to block online content deemed illegal or to be "violating privacy" of a person, without a court decision. It also forces Internet providers to keep records of users' activities for two years and make them available to authorities.

Prime Minister Recep Tayyip Erdogan has argued the overhauls will protect individuals' privacy and advance democracy by rooting out a "parallel state" within the bureaucracy he accuses of triggering a corruption case against his government. The bill's opponents—including the three parliamentary opposition parties and the European Union—say they are a crude bid to curb freedom of speech ahead of elections this year.

Although approval was broadly expected, the president's decision drew a firestorm of criticism online. Turks launched a campaign to unfollow the president on Twitter, calling on each other to "censor the censor."