2

I am making a small Web App which sends me reminders based on a URL parameter that it is passed. Currently, I have a personal domain - for argument's sake we'll call it http://somedomain.com.

I have a subdomain set up in my DNS settings on my domain provider's website so that it redirects to a Google Apps Script published Web Application which is designed to send me the reminder. My workflow is as follows:

  • I go to my subdomain at http://s.somedomain.com and pass a parameter using ? (for example http://s.somedomain.com?q=medicine)
  • s.somedomain.com is set up to redirect to my Apps Script web application located at https://script.google.com/macros/s/.../exec:
function doGet(e){
  
  GmailApp.sendEmail('MYEMAIL', 'Reminder: placeholder text', e.parameter.q);
  return HtmlService.createHtmlOutput("✔️ Sent");
}

The workflow works great in so much as the redirects all work and I end up at a page that just says ✔️ Sent. The issue here however, is that when I go to s.somedomain.com?q=medicine I get automatically redirected to s.somedomain.com/?q=medicine (with a / after the top level domain) which completely breaks the script, as the parameter is being passed to my root directory before redirect, causing the redirect to head to:

https://script.google.com/macros/s/.../exec/?q=medicine

and not:

https://script.google.com/macros/s/.../exec?q=medicine

I'm not 100% sure whether this is an issue with my DNS forward, or whether I will need to point my domain to a page and handle the parameters that way, or whether this can be done in Apps Script by trying to get the parameters after the trailing /.

My domain provider is ionos (formerly 1and1), and my destination settings are as can be seen below, under Domains & SSL > Subdomains > s.somedomain.com > Adjust Destination > Forward Domain.

Domain forwarding settings

Rafa Guillermo
  • 14,474
  • 3
  • 18
  • 54
  • Set up redirect to `https://script.google.com/macros/s/...`. Call `somedomain.com/exec?q=medicine`? – TheMaster Aug 09 '19 at 15:56
  • @TheMaster Great suggestion but it unfortunately doesn't work - calling `somedomain.com/exec?q=medicine` doesn't call the script and instead just redirects to `script.google.com/home`. I tried the domain-side redirect as you suggested, both without a trailing `/` after the script ID and then again with. I thought about handling it with some server-side redirects but due to my webserver circumstances I was hoping to do this in Apps Script or with DNS. I wonder if serverfault may be a good place to ask. – Rafa Guillermo Aug 09 '19 at 21:36
  • I've also edited my question as truth be told I'm testing this on a subdomain rather than the domain itself. Running on `s.subdomain.com`. – Rafa Guillermo Aug 09 '19 at 22:08
  • `somedomain.com/exec?q=medicine doesn't call the script and instead just redirects to script.google.com/home` Can you get network logs on that(should be in dev console)? Like what requests are made and where the redirect comes from? – TheMaster Aug 10 '19 at 06:51
  • 1
    @TheMaster `s.somedomain.com?q=medicine` gets `302 Found` which redirects to `https://script.google.com/macros/s/...` script's page without the `/exec?q=medicine`, followed by `script.google.com/a/macros/myDomain/s/...` and then finally `https://script.google.com/home`. All are `302 Found`. – Rafa Guillermo Aug 12 '19 at 07:27
  • To clarify, shouldn't `s.somedomain.com?q=medicine` be `s.somedomain.com/exec?q=medicine` Also, Shouldn't the script url already have your domain? `script.google.com/a/macros/myDomain/s/...` under `Domains & SSL > Subdomains > s.somedomain.com > Adjust Destination > Forward Domain.`? – TheMaster Aug 12 '19 at 07:56
  • @TheMaster Sorry, yes, you're right, I meant `s.somedomain.com/exec/?q=medicine` in my previous comment. `Under Domains & SSL > Subdomains > s.somedomain.com > Adjust Destination > Forward Domain` I have the script's web app deployment URL `https://script.google.com/macros/s/...` which doesn't contain my G-Suite myDomain, but as it appears when I go to `Publish > Deploy as web app > Update` in the script editor. My GSuite domain appears in the URL as a second redirect after going to `https://script.google.com/macros/s/...`. – Rafa Guillermo Aug 12 '19 at 09:53
  • @TheMaster Actually I feel that this can't be done just through redirects on my host provider's side. I went back and changed my redirect to `https://script.google.com/macros/s/.../exec` and used `s.somedomain.com?q=test` again instead of trying with Looking through the HAR file after attempting to go to `s.somedomain.com?q=test` shows that the parameter isn't getting passed along to `script.google.com/a/macros/myDomain/s/.../exec` so `Logger.log(parameters)` returns `{}`. – Rafa Guillermo Aug 12 '19 at 09:54

1 Answers1

0

This does not work with a pure redirect as URL parameters are not passed on redirect 302.

This can be done however by using the 'Frame redirect' option available on the ionos Forward Domain page. As suggested by TheMaster, rather than pointing the frame redirect to

https://script.google.com/macros/s/.../exec

It now redirects to:

https://script.google.com/macros/s/...

I have edited my code.gs file as the Apps Script web app is located on another domain and therefore can not be simply evaluated with HtmlService.createHtmlOutput():

function doGet(e){

  GmailApp.sendEmail('MYEMAIL', 'Reminder: placeholder text', e.parameter.q);
  return HtmlService.createTemplateFromFile('urlString').evaluate().setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL);
}

And I now have a urlString.html file which contains the confirmation page:

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
    ✔️ Sent    
  </body>
</html>
Rafa Guillermo
  • 14,474
  • 3
  • 18
  • 54