Testing AdSense on localhost

What is AdSense?

Google AdSense is a simple and popular way to earn money by displaying ads on your websites. AdSense is used by millions of websites, big and small. Most blogs can earn between $0.20-$15 per 1000 page views. You can integrate with AdSense by simply adding some small scripts to your websites.

Testing AdSense on localhost

In order to use Google AdSense, your domain needs to be reviewed and approved by Google first. After approval, you can start serving ads on the approved domain. Serving auto ads is as simple as enabling a switch on Google AdSense. Serving display ads on your desired locations will require a few more HTML + JavaScript changes.

Unfortunately, because AdSense will only serve ads on the approved domain, it will not show any ad on localhost (this restriction is true for many other ad services as well). So if you are trying to test out custom display ads placement locally, you will not see how the ads might be displayed, making development very difficult. Obviously you can blindly deploy your changes to production and hope for the best, but that is very risky, and each iteration is also very slow.

With ModResponse, you can reroute your production domain traffics to localhost, allowing you to truly preview your local changes on your production domain. Since the page is served from the production domain, Google AdSense will display ads correctly there, even though the codes are actually coming from localhost.

To do this, simply install ModResponse, then click on and select Reroute request. Enter your production domain, then enter the localhost and port in Reroute URL as followed:

This configuration above will reroute all https://modheader.com/* requests to http://localhost:3000/*, while keeping the path the same. The additional Tab filters allow me to restrict this change only to the selected tab. With this configuration, when I visit https://modheader.com/ on the selected tab, the HTTP request will be routed to localhost:3000 instead of the actual production service. To the browser though, the request is still coming from https://modheader.com/, so when Google AdSense is loaded, it will be able to display ads there if modheader.com is an approved domain. With this, I can customize the display ads location, size, and styles locally. I can make sure they look good before deploying to production.