Google site verification in Lift

Posted on Sep 20, 2016 by Alexej Bondarenko

Google Webmaster Tools verification (or any other verification) is a bit tricky in Lift when it comes to the file based verification process. Usually you'll have to upload a simple HTML file with a specific content to your server. Google will fetch this file from your server and check the contents provided.

If you use Lift behind Nginx (or any other proxy solutions) you might come up with the idea to let Nginx handle this request. And yes, this is a good solution which will do the trick. But what about the case when you want Lift explicitly handle those kind of requests?

The first obvious approach is to place the static HTML file (let's say google1d341eb1f8ec34dc.html) into your webapp folder and add a Sitemap entry for this file, like this:

Menu(Loc("Google Verification", "google1d341eb1f8ec34dc" :: Nil, "Webmaster Tools")) 

Now you can try to pass the verification, which will fail. But why? Because this way Lift will serve this file as stateful source, inject script tags and add data tags into your files content. Of course google will deny this content because it does not match the provided file.

Hence we need to tell Lift not to do this and serve the file as is. Let's remove the Menu entry again and instead add a new rule to our Boot:

LiftRules.liftRequest.append {
  case Req(List("google1d341eb1f8ec34dc"), "html", _) => false

This way verification will pass because Lift will hit this rule and just return plain file content to the client. You can use this approach to serve other static files as well.

I hope you find this short tutorial helpful. If you have any questions or comment please use the section below to leave a note or ping me on Twitter.