301如何重定向Google App Engine上的页面URL?

时间:2018-08-26 21:35:01

标签: google-app-engine redirect url-redirection google-app-engine-python

我正在更新手工编码的静态站点的文件结构,因为我最初是在一段时间以前对Web开发中的文件结构的最佳实践不甚了解的时候创建的。

我知道,为了获得最佳用户体验以及最佳SEO用途,应尽量少使用重定向。

我通常在Stack Overflow和Google上进行搜索,以找到一种为Google App Engine(GAE)上的单个静态页面URL设置301重定向的解决方案,但是我似乎只能找到整个域名重定向问题。

该网站是通过html,css和js手工编码的,并集成了Bootstrap v3.3.7,以实现移动响应。

该网站托管在Google App Engine上,并且是.appspot.com(http://christopher-john-roberts.appspot.com)的子域。

例如,我将移动以下页面:

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

到新网址:

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

仍低于http://christopher-john-roberts.appspot.com/

我的 app.yaml 如下:

 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/(.*)

我的 main.py 文件如下:

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 ()

如果需要编写脚本,我宁愿实施python解决方案,因为这是GAE上的当前部署。

不要犹豫,问我是否错过了任何重要信息。

预先感谢您的回复。


更新

我现在尝试编辑main.py文件以引入self.redirect,如下所示:

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 ()

但是,这没有用。访问网址http://www.christopher-john-roberts.appspot.com/content/scientific-skills/mres-post-genomic-science.html时,我没有被重定向,而是得到错误:未找到 在此服务器上找不到请求的URL /content/scientific-skills/mres-post-genomic-science.html。

我也尝试过:

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 ()

还有...

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 ()

更新2

我在下面提供了当前main.py文件的屏幕截图:

screenshot of main.py file


更新3

我最新的main.py文件:

latest screenshot of main.py

日志:

screenshot of logs

1 个答案:

答案 0 :(得分:1)

Private Sub ShoppingCartPage_Load(sender As Object, e As EventArgs) Handles Me.Load If Not (IsPostBack) Then If Not (Request.QueryString.ToString().Length.Equals(0)) Then Dim newProduct As Product = New Product(Request.QueryString("ProductCode"), Request.QueryString("ProductName"), Request.QueryString("Category"), Val(Request.QueryString("Price")), Request.QueryString("Description")) If Session("shoppingCartSession") Is Nothing Then shoppingCart = New ArrayList() shoppingCart.Add(newProduct) Session("shoppingCartSession") = shoppingCart ElseIf (shoppingCart.Contains(newProduct.itemID)) Then For Each item As Product In shoppingCart If item.Equals(Request.QueryString("ProductCode")) Then item.updateQuantity() End If Next Else shoppingCart = CType(Session("shoppingCartSession"), ArrayList) shoppingCart.Add(newProduct) Session.Add("shoppingCartSession", shoppingCart) End If End If shoppingCart = CType(Session("shoppingCartSession"), ArrayList) createShoppingCartTable() End If End Sub 中内置了self.redirect。将此添加到您的webapp

def get(self, q)