title: Cloud function for firebase to redirect to a URL
body: <p>I am trying to create a download endpoint for my mobile app. The app is available on app store and play store. I want to have a single URL that can be used by users to download the app on either an iOS device or an Android device. I was trying to find out if I can create a cloud function for firebase for this purpose. I was thinking of checking the <code>user-agent</code> for the request and based on whether the request came from an iOS device or an Android device or a web browser, redirect the user to an appropriate URL. I have verified that the device information is present in the <code>user-agent</code> header. For e.g.</p>
  
  <pre><code>"user-agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 10_2 like Mac OS X) AppleWebKit/602.3.12 (KHTML, like Gecko) Version/10.0 Mobile/14C89 Safari/602.1 AppEngine-Google; (+http://code.google.com/appengine; appid: s~gcf-http-proxy)"
  </code></pre>
  
  <p>I guess the next things to do was to redirect the user to the app store URL in the above case. I tried to use <code>res.redirect</code> but that doesn't work. That ends up redirecting the user to <code>cloud-function-base-url/redirect-url</code> instead of just <code>redirect-url</code>.</p>
  
  <p>Below is the code for the cloud function which is just trying to redirect to www.google.com when the function is triggered from the browser.</p>
  
  <pre><code>exports.appDownload = functions.https.onRequest((req, res) =&gt; {
    console.log(stringify(req.headers))
    // This will eventually redirect to a store URL based on the phone in user-agent
    res.redirect('www.google.com');
  });
  </code></pre>
  
  <p>This ends up redirecting the browser to <code>base-url/www.google.com</code> instead of just <code>www.google.com</code> and I get an error message saying <code>Not Found</code>. I wanted to redirect the browser to just <code>www.google.com</code>.</p>
  
  <p>I wanted to check if it is possible to achieve the above using a cloud function for firebase.</p>