跳转到主要内容

使用Python为Plone CMS编写重定向

项目描述

简介

collective.scriptedredirect 允许您使用Python代码为您Plone CMS站点编写HTTP 302临时移动和HTTP 301永久移动重定向。

https://travis-ci.org/collective/collective.scriptedredirect.png

优点

  • 重定向逻辑不依赖于前端Web服务器:无需触摸Apache、Varnish或Nginx的各种配置文件)

  • Python允许更容易地编写更复杂的重定向逻辑 - 不需要正则表达式!

  • Plone中的Python脚本可以访问更完整的状态信息:用户登录状态、权限等。

使用方法

安装

buildout.cfg中添加附加组件

eggs =
    ...
    collective.scriptedredirect

运行buildout。

在“站点设置”>“附加组件”中安装“Python脚本重定向”。

通过Web进行重定向

在网站根目录的Zope管理界面中编辑redirect_handler

http://cloud.github.com/downloads/collective/collective.scriptedredirect/Screen%20Shot%202012-09-25%20at%201.28.18%20AM.png

在意外情况下,使用?no_redirect HTTP查询参数来覆盖重定向器并修复您的网站。

通过Web进行重定向

您还可以注册一个名为redirect_handler的浏览器视图。在这种情况下,您在附加Python代码中编写重定向代码,而不是通过Web。

redirect_handler视图始终优于redirect_handler脚本。

redirector.py中的示例Python代码

class TestingRedirectHandler(object):
    """ Redirect handler registered as a ``redirect_handler`` Zope 3 <browser:page>
    """

    def __init__(self, context, request):
        self.context = context
        self.request = request

    def __call__(self, url, host, port, path):
        """
        :return: None if no redirect needed, otherwise a string full HTTP URL to the redirect target

        :raise: zExceptions.Redirect or other custom redirect exception if needed
        """

        # Simple example: always access site over www. domain prefix
        if not url.startswith("http://www."):
            return url.replace("http://", "http://www.")

        # Don't redirect if we are already using www. prefix
        return None

示例ZCML

<browser:page
    name="redirect_handler"
    for="Products.CMFCore.interfaces.ISiteRoot"
    layer="YOUR_ADDON_LAYER"
    class=".redirector.TestRedirectHandler"
    />

内部结构

collective.scriptedredirect将其自身挂钩到Zope的预遍历挂钩,并在请求遍历Zope应用程序服务器中的Plone站点之前触发。

作者

Mikko Ohtamaa (博客, Facebook, Twitter, Google+)

变更日志

1.1.1 (2013-04-14)

  • 为PyPi固定.rst标题级别 [miohtama]

1.1 (2013-04-14)

  • 添加通过浏览器视图支持的重定向 [miohtama]

  • 测试,Travis CI支持 [miohtama]

1.0

  • 初始开发 [miohtama]

项目详情


下载文件

下载适用于您的平台的文件。如果您不确定选择哪个,请了解有关安装包的更多信息。

源分发

collective.scriptedredirect-1.1.1.zip (36.8 kB 查看哈希值)

上传时间

由以下支持