"We don't like that Mr Cameron is going to Gibraltar," Rajoy said, according to El Pais.
"What is at stake is whether the UK stays in the EU, or leaves the EU, and political campaigns on that issue should take place in the UK, not in Gibraltar."
With just one week to go before the referendum, and with the "Leave" camp taking the lead in the polls, Prime Minister Cameron decided to fly to the British overseas territory in southern Spain — to spur Gibraltar's 23,000 UK citizens to vote "Remain" and to address Gibraltarians' concerns about the consequences of a Brexit.
While the residents of Gibraltar are fiercely nationalistic and fond of British traditions, they are expected to vote en masse for remaining.
That is mainly due to fears that — if Brexit were to happen — Spain could close its borders with Gibraltar, effectively isolating the territory.
Right now — as a part of the UK and therefore, of the EU — Gibraltar has close relations with its Spanish neighbors, with over 10,000 people commuting across the border every day. Some worry that the only way to keep the border open would be accepting a joint British-Spanish rule on the territory, a perspective loathed by most Gibraltarian Britons.
Cameron's 2-hour trip is the first time a British PM visits Gibraltar since 1968 and, while largely symbolic, it is rekindling old grudges in Spain. Madrid sees Gibraltar as a colonialist relic that blemishes Spain's territorial unity.
The Spanish government made clear its "opposition to these kind of visits [on the part of the British PM]" and ordered its ambassador in the UK, Federico Trillo, to hammer the point home.
Rajoy — who is still formally running the country after last December's inconclusive election and ahead of next week's new round of elections — said Madrid would not take any additional measure to condemn Cameron's visit.