Chris_Roberts
Chris_Roberts

Reputation: 31

How can I 301 redirect a page URL on Google App Engine?

I am updating the file structure of my hand coded static site, as I originally created it a while back when I had less of an appreciation of best practices for file structure in web development.

I am aware that for best user experience, as well as optimal SEO purposes, redirects should be used minimally.

I have searched Stack Overflow and Google in general for a solution for setting up 301 redirects for individual static page URL's on Google App Engine (GAE), however I only seem to be able to find entire domain name redirect explinations.

The site is hand coded in html, css and js with Bootstrap v3.3.7 integrated for mobile responsiveness.

The site is hosted on Google App Engine and is a subdomain of .appspot.com (http://christopher-john-roberts.appspot.com).

I will be for example moving the following page:

http://christopher-john-roberts.appspot.com/content/scientific-skills/mres-post-genomic-science.html

to the new URL:

http://christopher-john-roberts.appspot.com/scientific-skills/mres-post-genomic-science.html

still under http://christopher-john-roberts.appspot.com/

My app.yaml is as follows:

 runtime: python27
 api_version: 1
 threadsafe: true

 handlers:
 - url: /
   static_files: www/index.html
   upload: www/index.html

 - url: /(.*)
   static_files: www/\1
   upload: www/(.*)

My main.py file is as follows:

import os
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
from google.appengine.ext.webapp import template

class MainHandler(webapp.RequestHandler):
  def get (self, q):
    if q is None:
      q = 'index.html'

    path = os.path.join (os.path.dirname (__file__), q)
    self.response.headers ['Content-Type'] = 'text/html'
    self.response.out.write (template.render (path, {}))

def main ():
  application = webapp.WSGIApplication ([('/(.*html)?', MainHandler)], debug=True)
  util.run_wsgi_app (application)

if __name__ == '__main__':
  main ()

If scripting is required I would prefer to implement a python solution as that is the current deployment on GAE.

Don't hesitate to ask if I have missed out any vital information.

Thanks in advance for your response.


UPDATE

I have now tried editing my main.py file to introduce a self.redirect as follows:

import os
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
from google.appengine.ext.webapp import template

class MainHandler(webapp.RequestHandler):
  def get (self, q):
    if q is None:
      q = 'index.html'

    if q = 'content/scientific-skills/mres-post-genomic-science.html':
      redirect_url = 'http://christopher-john-roberts.appspot.com/scientific-skills/mres-post-genomic-science.html'
      self.redirect(redirect_url, permanent=True)         

    path = os.path.join (os.path.dirname (__file__), q)
    self.response.headers ['Content-Type'] = 'text/html'
    self.response.out.write (template.render (path, {}))  

def main ():
  application = webapp.WSGIApplication ([('/(.*html)?', MainHandler)], debug=True)
  util.run_wsgi_app (application)

if __name__ == '__main__':
  main ()

However this did not work. When visiting the url http://www.christopher-john-roberts.appspot.com/content/scientific-skills/mres-post-genomic-science.html I am not redirected and instead get Error: Not Found The requested URL /content/scientific-skills/mres-post-genomic-science.html was not found on this server.

I have also tried:

import os
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
from google.appengine.ext.webapp import template

class MainHandler(webapp.RequestHandler):
  def get (self, q):
    if q is None:
      q = 'index.html'      

    path = os.path.join (os.path.dirname (__file__), q)
    self.response.headers ['Content-Type'] = 'text/html'
    self.response.out.write (template.render (path, {}))  

    if q = 'content/scientific-skills/mres-post-genomic-science.html':
      redirect_url = 'http://christopher-john-roberts.appspot.com/scientific-skills/mres-post-genomic-science.html'
      self.redirect(redirect_url, permanent=True)       

def main ():
  application = webapp.WSGIApplication ([('/(.*html)?', MainHandler)], debug=True)
  util.run_wsgi_app (application)

if __name__ == '__main__':
  main ()

As well as ...

import os
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
from google.appengine.ext.webapp import template

class MainHandler(webapp.RequestHandler):
  def get (self, q):
    if q is None:
      q = 'index.html'      

    if q = 'content/scientific-skills/mres-post-genomic-science.html':
      redirect_url = 'scientific-skills/mres-post-genomic-science.html'
      self.redirect(redirect_url, permanent=True)        

    path = os.path.join (os.path.dirname (__file__), q)
    self.response.headers ['Content-Type'] = 'text/html'
    self.response.out.write (template.render (path, {}))       

def main ():
  application = webapp.WSGIApplication ([('/(.*html)?', MainHandler)], debug=True)
  util.run_wsgi_app (application)

if __name__ == '__main__':
  main ()

UPDATE 2

I have provided a screenshot below of my current main.py file:

screenshot of main.py file


UPDATE 3

My latest main.py file:

latest screenshot of main.py

The logs:

screenshot of logs

Upvotes: 1

Views: 1462

Answers (1)

GAEfan
GAEfan

Reputation: 11360

There is a self.redirect built right into webapp. Add this to your def get(self, q):

logging.info(q)

if q == 'content/scientific-skills/mres-post-genomic-science.html':

    redirect_url = 'http://christopher-john-roberts.appspot.com/scientific-skills/mres-post-genomic-science.html'

    self.redirect(redirect_url, permanent=True)

Upvotes: 1

Related Questions