According to general relativity, the frequency of electromagnetic radiation is altered by the expansion of the universe. This effect--commonly referred to as the cosmological redshift--is of utmost importance for observations in cosmology. Here we show that this redshift can be reproduced on a much smaller scale using an optical analogue inside a dielectric metamaterial with time-dependent material parameters. To this aim, we apply the framework of transformation optics to the Robertson-Walker metric. We demonstrate theoretically how perfect redshifting or blueshifting of an electromagnetic wave can be achieved without the creation of sidebands with a device of finite length.