Tutorial :Switch off case sensitive URLs in Google App Engine



Question:

We recently moved our company website to Google app engine. We have encountered case sensitivity problems with some of the links in our website. Some links are uppercase when the corresponding folders on the server are lowercase. This was not an issue on our old windows server. Google app engine appears to be case sensitive with URLs. This is causing broken links.

Does anyone know if there is a way to make our URLs work case insensitively on Google app engine?


Solution:1

Is this for static files or dynamic handlers? for dynamic handlers, you can easily write a piece of WSGI middleware that lower-cases all URIs:

def lower_case_middleware(environ, start_response):    environ['SCRIPT_NAME'] = environ['SCRIPT_NAME'].lower()    environ['PATH_INFO'] = environ['PATH_INFO'].lower()    return application(environ, start_response)  

Note that this isn't a 'bug' in App Engine - URLs are case sensitive, and the only reason things did work is because Windows, unlike most other platforms, ignores case.

For static files, add a static handler that only accepts lower case filenames, and a dynamic handler that accepts filenames of either case:

handlers:  - url: /static/([^A-Z]+)    static_files: static/\1    upload: static/.*  - url: /static/.*    handler: tolowercase.py  

Now write 'tolowercase.py', a handler that redirects any mixed case filename to the lower-cased version:

from google.appengine.ext import webapp  from google.appengine.ext.webapp.util import run_wsgi_app    class LowerCaseRedirecter(webapp.RequestHandler):    def get(self, path):      self.redirect('/static/%s' % (path.lower(),))    application = webapp.WSGIApplication([('/static/(.*)', LowerCaseRedirecter)])    def main():    run_wsgi_app(application)    if __name__ == "__main__":    main()  

Edit: Added a solution for static files.


Solution:2

I don't know of a built in way.

All I can think is that you would need to create a handler for /(.*) and then write some code to forward on the requests to the correct handlers from there.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »