11

I am incredibly confused here. I've been using Google Appengine for our webserver and it's been working out great so far, but I had a request to have the webpages load without the .html extension. I've been doing some reading and see that I need to create an app.yaml file to map the url to something else? This is what I have in my file so far:

application: company-website
version: 1
runtime: java
threadsafe: true

handlers:
  - url: /about
  script: about.html

I've been trying to read how to do this on their documentation site but I can't seem to find anything referencing how to remove the extension and still have it point to the right html file. Can anyone help me out? Can I just do this in the appengine-web.xml file, also? It seems like I could just do it in there without creating an app.yaml file.

Any help would be appreciated. Thanks!

edit: tried some more things.. Tried moving the file I'm trying to remove the extension on to its own folder like so:

/root
  -index.html
  -/about
     -index.html

And this was OK, when I typed my domain.com/about/ it appears to be working but when I typed domain.com/about it does not. Very frustrating.

shan
  • 3,035
  • 5
  • 34
  • 50
  • Have a look at this solution: http://stackoverflow.com/a/20220633/3245762 – hbelmiro Sep 15 '15 at 01:55
  • that answer is for python, when I try this in Java it does not recognize that mime_type is a property of handlers so I do not believe that Java has these options – shan Sep 15 '15 at 01:58
  • I don't know about java, but in python you can set `strict_slash=True` which indicates that you need or need not type the extra `/` – Tim Sep 15 '15 at 07:22
  • Regarding your edit: your `url` in handlers would have to look like `url: /about.*` if redirection of everything that starts with "about" should go to about/index.html. Not sure about the `script` part though. Logically it should be `static_files: about/index\.html` instead, but that is my sense of regular expressions and mod_rewrite. – konqi Nov 26 '15 at 09:02

2 Answers2

3

In an ideal world we would have the option to configure a rewrite like apache's mod_rewrite in the underlying web server. Sadly it is not possible to configure a rewrite on such a level.

I searched around a bit and found that the most common answer for a rewrite is to user either UrlRewriteFilter or to wire up the servlets yourself. Both options are explained in

Both work in the same way and will require the app to serve static content through app engine. This will result in app engine instance hours and slower responses since all you static files move from Google's content delivery network (cdn) to your bottleneck app. The aproaches possibly also require you to deploy your static files as resource instead (How-To configure static-files and resources), at least that is how i have done this before.

These are the 'pure Java' options you have. The app.yaml approach that Josep Valls described will work in with Java on App Engine. The main question here is if the app.yaml configuration is low level enough to be a rewrite that google recognizes in its cdn, or whether you'll still burn through instance hours because all content is served through instances.

The documentation tells us:

For efficiency, App Engine stores and serves static files separately from application files. Static files are not available in the application's file system. If you have data files that need to be read by the application code, the data files must be application files, and must not be matched by a static file pattern.

Since this comes right before the section that explains how to configure the static file pattern handlers one should assume that the configuration of such handlers will not break the logic that is mentioned above - that is

stores and serves static files separately from application files

Whether this assumption is correct is an easy experiment which i shall conduct given time and report my findings here.

These are all the existing options I could find and know of. If anyone knows more on this topic, please comment / respond.

EDIT (7.12.2015) My maven target appengine:devserver is completely oblivious to settings in the app.yaml. I'll have to experiment with this during one of the next deployment phases or use mvn gcloud:run.

... later that day:

Rewriting the URL via Servlet (like with Paul Tuckey's UrlRewriteFilter) does not work for static files. You would have to deploy the files as resource files. Static files reside somewhere else and will not be found if forwarded to by a servlet. At least that's how i understand it.

Community
  • 1
  • 1
konqi
  • 5,137
  • 3
  • 34
  • 52
2

In Python and Go you can use regular expression matching in your url handlers; if Java also uses app.yaml you could probably do this:

- url: /(about|other|sections)$
  static_files: \1\.html
Josep Valls
  • 5,483
  • 2
  • 33
  • 67
  • Thanks! app.yaml works in Java runtime, but it will cause overwriting of web.xml and appengine-web.xml files that are normally used for configuring a Java app. (Source: https://books.google.com/books?id=64oNCgAAQBAJ&pg=PA57&lpg=PA57&dq=app+engine+java+app.yaml). BTW, what is the role of `\1` in the above expression? – Price Nov 22 '15 at 18:31
  • 1
    when you use parenthesis in your regular expression, you can refer to each of the matches using \1--\99; i.e. if your regex is something like /(\w+)/(\w+)/ then you can use \1 to replace the first part of the path and \2 the second part. See more here: http://www.rexegg.com/regex-capture.html – Josep Valls Nov 22 '15 at 22:32
  • @JosepValls I'm curious. We (all) know that static content on app engine is cached and delivered through Googles CDN. However if you would rewrite a URL with a Java servlet the request would have to be routed through app engine curcumventing the CDN. In your suggested approach would the request frist hit an app engine instance or is the configuration low level enough (like a server configuration) to expose the rewritten content as static and is therefor cached and served by the CDN? Short: Will the rewrite cause app engine to spawn instances? – konqi Nov 24 '15 at 08:52
  • @konqi that is an interesting question for which I don't have an answer. I wonder if static_dir and static_files are handled the same. My guess is that it will need a running instance to map your regex. However if you plan on abusing GAE as a CDN it may not be the best: http://serverfault.com/questions/133157/using-googles-app-engine-as-cdn-for-static-files – Josep Valls Nov 30 '15 at 21:27