我正在将我的公司网站转移到谷歌应用程序引擎,它主要是静态内容,其中有一些小部分,例如使用 python 生成的日期。我已经正确设置了所有内容,并且它在应用程序引擎上运行良好。现在我想做一些与 SEO 相关的 URL 更改。
这是现在提供网站服务的代码行。
app = webapp2.WSGIApplication([
('/', IndexPage),
('/discover', DiscoverPage),
('/about', AboutPage),
('/help', HelpPage),
('/terms-and-privacy', TermsPage)
], debug=True)
为每个页面定义这样的类。
class DiscoverPage(webapp2.RequestHandler):
def get(self):
template_values = {
'bodyclass': 'discover',
}
template = JINJA_ENVIRONMENT.get_template('discover.html')
self.response.write(template.render(template_values))
现在我想要实现的目标是:
- 当从
www.domain.com
访问网站时,我想将其重定向到domain.com
。
I have added both www and non www mappings at the app engine developer console, the site is currently accessible from both www and non www urls.but I only want
non www
version
- 当网址尾部有斜杠时,将其删除并发送到不带尾部斜杠的版本。
Right now
domain.com/discover
works fine butdomain.com/discover/
ends up in 404 .
我对 python webapps 没有太多经验,我的背景主要是 apache/nginx
服务器和 php
。 AppEngine 是否有诸如 htaccess 规则
或 nginx 重写
之类的内容?
最佳答案
您可以首先捕获对“www”子域的所有请求:
from webapp2_extras.routes import DomainRoute
app = webapp2.WSGIApplication([
DomainRoute('www.domain.com', [
webapp2.Route(r'/<:.*>', handler=RedirectWWW),
]),
('/', IndexPage),
('/discover', DiscoverPage),
('/about', AboutPage),
('/help', HelpPage),
('/terms-and-privacy', TermsPage)
], debug=True)
使用一个处理程序将 www 部分替换为裸域:
class RedirectWWW(webapp2.RequestHandler):
def get(self, *args, **kwargs):
url = self.request.url.replace(self.request.host, 'domain.com')
return self.redirect(url, permanent=True)
def post(self, *args, **kwargs):
return self.get()
关于第二个问题,您可以在此处阅读有关 strict_slash
参数的信息:https://webapp-improved.appspot.com/api/webapp2_extras/routes.html
关于python - 如何对 google AppEngine Webapp2 URL 进行 SEO Url 更改和重定向?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30013236/