Search code examples
htmlgoogle-app-engine

Error 404 when loading subfolder on Google app engine


I have a website with an index.html as a homepage that works fine, but I want to create a website structure with subfolders, so I created a folder named "team".

Inside "team", I put an index.html file with the team information. When I load www.mysite.com/team/index.html it works fine, but if I load www.mysite.com/team I get a 404 error.

How do I fix this to load the index.html inside the subfolder automatically??

I am using the Google app engine as a server, I upload files with the Google app engine launcher and use some Python files.


Solution

  • In you app.yaml, define your static file handlers like this:

    …
    handlers:
    - url: /static
      static_dir: static
    - url: /(.*?)/?
      static_files: \1/index.html
      upload: (.*?)/index.html
    …
    

    Note that because you define your website structure like this, you won't be able to access any other file except the index.html files. For example, if you go to /images/logo.png, App Engine would try to serve that from the static file images/logo.png/index.html.

    To fix that problem, you'll need to put all your static files in a different subdirectory (/static in the above example), and serve that as a static_dir. Then you should reference your files from the index.html files as /static/images/logo.png, etc.

    UPDATE: I'll paste your app.yaml here because you cannot format it correctly in the comment:

    handlers:
    - url: /(.*\.(gif|png|jpg|ico|js|css|php|xml))
      static_files: \1
      upload: (.*\.(gif|png|jpg|ico|js|css|php|xml))
    
    - url: /robots.txt
      static_files: robots.txt
      upload: robots.txt 
    
    - url: /favicon.ico
      static_files: favicon.ico
      upload: favicon.ico
    
    - url: .*
      script: main.py
    
    - url: /static
      static_dir: static
    
    - url: /(.*?)/?
      static_files: \1/index.html
      upload: (.*?)/index.html
    

    Your problem is that the - url: .* directive catches everything so the bottom two handlers are never reached.

    Also, you should consider using an application reference in the script instead of the file name. Providing the script file name is deprecated.